网站建设与网页设计开题报告,重生主角做视频网站的小说,做网站困难嘛,石家庄网站建设方案本文章就讲的是很基础的类的使用 重点大概就是类的构造函数以及一些很基础的东西。
实验内容是些老生常谈的东西#xff0c;Complex类#xff0c;在当初学C面向对象的时候也是这个样子展开的。 内容如以下#xff1a;
public class Complex {float real;float imag;public…本文章就讲的是很基础的类的使用 重点大概就是类的构造函数以及一些很基础的东西。
实验内容是些老生常谈的东西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.real0){str this.imag i;}else str this.real this.imag i;}else{if(this.real0){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.real0){str this.imag i;}else str this.real this.imag i;}else{if(this.real0){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);}
}
就这样了~