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

文章介绍了C++中的类基础,特别是构造函数的使用和重载概念。以Complex类为例,展示了如何定义和使用包含实部和虚部的复数类,包括加减乘运算以及toString方法。同时,文章强调了构造函数的重载,即通过不同的参数列表实现不同功能的同名函数。
摘要由CSDN通过智能技术生成

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

实验内容是些老生常谈的东西,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);
    }
}

就这样了~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泉绮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值