当前位置: 首页 > news >正文

Java实验课的学习笔记(二)类的简单使用

本文章就讲的是很基础的类的使用
重点大概就是类的构造函数以及一些很基础的东西。

实验内容是些老生常谈的东西,Complex类,在当初学C++面向对象的时候也是这个样子展开的。
内容如以下:

public class Complex {float real;float imag;public Complex(){real = 0;imag = 0;}public Complex(float real, float imag){this.real = real;this.imag = imag;}public Complex add(float real){this.real += real;return this;}public Complex add(Complex complex){this.real += complex.real;this.imag += complex.imag;return this;}public Complex sub(float real){this.real -= real;return this;}public Complex sub(Complex complex){this.real -= complex.real;this.imag -= complex.imag;return this;}public Complex mul(float real){this.real *= real;this.imag *= real;return this;}public Complex mul(Complex complex){float TheReal = this.real;float TheImag = this.imag;this.real = TheReal * complex.real - TheImag*complex.imag;this.imag = TheReal * complex.imag + TheImag*complex.real;return this;}public String toString(){String str="";if(this.imag == 0){str += this.real;}else if(this.imag > 0 ){if(this.real==0){str += this.imag + "i";}else str += this.real + "+" + this.imag + "i";}else{if(this.real==0){str += this.imag + "i";}else str += this.real + "" + this.imag + "i";}return str;}}

这里也不得不说一丁点相关知识–重载 overload
如这里的:

	public Complex(){real = 0;imag = 0;}public Complex(float real, float imag){this.real = real;this.imag = imag;}public Complex add(float real){this.real += real;return this;}public Complex add(Complex complex){this.real += complex.real;this.imag += complex.imag;return this;}

这里的Complex()Complex(float real, float imag)构造函数也就用了一些重载的知识。
所谓重载,就是函数名不变但是函数内部的参数变化,我们调用函数时通过函数里面的参数决定我们要调用哪个函数。
同理的,Complex add(float real),Complex add(Complex complex)也是如此,当add()的括号里面是一个float值的时候调用Complex add(float real),括号里面是一个Complex对象的时候调用Complex add(Complex complex)
我当时认为这不是理所当然的吗?其实不是的,这里是用到了重载(overload)的知识。
总代码:
在这里插入图片描述

包com.Class.Work3里的Complex类:

package com.Class.Work3;public class Complex {float real;float imag;public Complex(){real = 0;imag = 0;}public Complex(float real, float imag){this.real = real;this.imag = imag;}public Complex add(float real){this.real += real;return this;}public Complex add(Complex complex){this.real += complex.real;this.imag += complex.imag;return this;}public Complex sub(float real){this.real -= real;return this;}public Complex sub(Complex complex){this.real -= complex.real;this.imag -= complex.imag;return this;}public Complex mul(float real){this.real *= real;this.imag *= real;return this;}public Complex mul(Complex complex){float TheReal = this.real;float TheImag = this.imag;this.real = TheReal * complex.real - TheImag*complex.imag;this.imag = TheReal * complex.imag + TheImag*complex.real;return this;}public String toString(){String str="";if(this.imag == 0){str += this.real;}else if(this.imag > 0 ){if(this.real==0){str += this.imag + "i";}else str += this.real + "+" + this.imag + "i";}else{if(this.real==0){str += this.imag + "i";}else str += this.real + "" + this.imag + "i";}return str;}}

调用Complex类的TestWork3类:

import com.Class.Work3.Complex;public class TestWork3 {public static void main(String[] args) {Complex c = new Complex(1, 2);
//        c.add(5);
//        Complex w = new Complex(4,1);
//        c.sub(w);
//        Complex w2 = new Complex(1,2);
//        c.mul(w2);
//        c.mul(5);System.out.println(c);}
}

就这样了~

http://www.lryc.cn/news/60153.html

相关文章:

  • 实战案例|聚焦攻击面管理,腾讯安全威胁情报守护头部券商资产安全
  • c++算法初级8——递推
  • Java后端面试题 重难点和被问到没答上来的点(包括java基础、关系型数据库、Redis、计算机网络、Spring、Java多线程、vue等)
  • 易观千帆 | 2023年3月银行APP月活跃用户规模盘点
  • [Android+JetPack] (Java实现) Retrofit2+RxJava3+Paging3+RecyclerView 实现加载网络数据例子 记录
  • Java 解析配置文件注入到配置类属性中供全局使用【开发记录】
  • 【Python开发手册】深入剖析Google Python开发规范:规范Python注释写作
  • Python入门教程+项目实战-9.3节: 字符串的操作方法
  • ENVI 5.6软件安装教程
  • 在Windbg中设置断点追踪打开C++程序远程调试开关的模块
  • CRM客户管理软件开发功能有哪些?
  • C++函数式魔法之旅(Journey of Functional Magic)
  • Vue基础入门(上)
  • 字符串匹配—KMP算法
  • 【微信小程序】 权限接口梳理以及代码实现
  • 【每日一词】leit-motif
  • windows 环境修改 Docker 存储目录
  • 上海市青少年算法月赛丙组—目录汇总
  • 手动实现promise.all
  • 如何搭建关键字驱动自动化测试框架?这绝对是全网天花板的教程
  • 字符串反转操作
  • TensorFlow 智能移动项目:1~5
  • [MAUI 项目实战] 手势控制音乐播放器(四):圆形进度条
  • web路径专题+会话技术
  • Jetpack Compose 实战 宝可梦图鉴
  • 高效时间管理日历 DHTMLX Event Calendar 2.0.3 Crack
  • ASIC-WORLD Verilog(2)FPGA的设计流程
  • 数字化体验时代,企业如何做好内部知识数字化管理
  • Qt5.12實戰之Linux靜態庫與動態庫多文件生成a與so文件並調用
  • Spring 之初始化前中后详解