本文章就讲的是很基础的类的使用
 重点大概就是类的构造函数以及一些很基础的东西。
实验内容是些老生常谈的东西,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);
    }
}
 
就这样了~



















