【JAVA面向对象练习】---第四天

数据求和类

定义一个类Demo,其中定义一个求两个数据和的方法,定义一个测试了Test,进行测试。

java 复制代码
package com.fuhai.day04;

public class Sum {
    private double a;
    private double b;

    public double getA() {
        return a;
    }

    public void setA(double a) {
        this.a = a;
    }

    public double getB() {
        return b;
    }

    public void setB(double b) {
        this.b = b;
    }

    // 无参构造
    public Sum(){

    }
    // 有参构造
    public Sum(double a,double b){
        this.a = a;
        this.b = b;
    }

    public void show(){
        System.out.println("两数之和为:"+(this.a+this.b));
    }
}

长方形类

定义一个长方形类,定义 求周长和面积的方法,然后定义一个测试了Test2,进行测试。

java 复制代码
package com.fuhai.day04;

public class Rectangle {
    private double r_long;
    private double r_wide;

    public double getR_long() {
        return r_long;
    }

    public void setR_long(double r_long) {
        this.r_long = r_long;
    }

    public double getR_wide() {
        return r_wide;
    }

    public void setR_wide(double r_wide) {
        this.r_wide = r_wide;
    }


    public Rectangle(){

    }

    public Rectangle(double a,double b){
        this.r_long = a;
        this.r_wide = b;
    }


    public void perimeter(){
        System.out.println("周长为:"+((this.r_wide+this.r_long)*2));
    }

    public void sqrt(){
        System.out.println("面积为:"+(this.r_wide*this.r_long));
    }


}

员工类

定义一个员工类,自己分析出几个成员,然后给出成员变量,构造方法,getXxx()/setXxx()方法,以及一个显示所有成员信息的方法。并测试。

java 复制代码
package com.fuhai.day04;

public class Stuff {
    private String stuffName;
    private long stuffId;
    private String factory;

    public String getStuffName() {
        return stuffName;
    }

    public void setStuffName(String stuffName) {
        this.stuffName = stuffName;
    }

    public long getStuffId() {
        return stuffId;
    }

    public void setStuffId(long stuffId) {
        this.stuffId = stuffId;
    }

    public String getFactory() {
        return factory;
    }

    public void setFactory(String factory) {
        this.factory = factory;
    }

    public Stuff(){
    }
    public Stuff(String name,long sid,String factory){
           this.stuffName = name;
           this.stuffId = sid;
           this.factory = factory;
    }


    public void show(){
        System.out.println("员工的姓名为:"+this.stuffName+" 员工的ID为:"+this.stuffId+" 员工所在的车间为:"+this.factory);
    }


}

MyMath类

定义一个类MyMath,提供基本的加减乘除功能,然后进行测试。

java 复制代码
package com.fuhai.day04;

public class MyMath {

    private double a;
    private double b;

    public double getA() {
        return a;
    }

    public void setA(double a) {
        this.a = a;
    }

    public double getB() {
        return b;
    }

    public void setB(double b) {
        this.b = b;
    }

    public MyMath(){

    }
    public MyMath(double a,double b){
        this.a = a;
        this.b = b;
    }

    public void sum(){
        System.out.println("和为:"+(this.a + this.b));
    }

    public void sub(){
        System.out.println("差为:"+(this.a - this.b));
    }
    public void multi(){
        System.out.println("乘为:"+(this.a * this.b));
    }

    public void division (){
        System.out.println("除为:"+(this.a / this.b));
    }
}

测试

java 复制代码
Sum sum = new Sum(3, 6);
sum.show();


Rectangle rectangle = new Rectangle(3, 5);
rectangle.perimeter();
rectangle.sqrt();


Stuff stuff1 = new Stuff("张三", 342345443, "第三车间");
Stuff stuff2 = new Stuff("李四", 342345444, "第二车间");
Stuff stuff3 = new Stuff("王五", 342345445, "第一车间");
Stuff stuff4 = new Stuff("李六", 342345446, "第四车间");
stuff1.show();
stuff2.show();
stuff3.show();
stuff4.show();

MyMath myMath = new MyMath(23, 56);
myMath.sum();
myMath.sub();
myMath.multi();
myMath.division();

结果输出

相关推荐
程序喵大人25 分钟前
【C++进阶】STL算法与函数对象 - 02 sort为什么需要随机访问迭代器
开发语言·c++·算法
SomeB1oody35 分钟前
【RustyML入门】2.6. 线性判别分析
开发语言·后端·机器学习·rust·教程
程序员雷欧1 小时前
环形缓冲区深度解析:从基础原理到Disruptor源码的全面剖析
java
xiaoqiMikko1 小时前
Dependabot 面板全绿,不代表你的 Tomcat 没洞
java·spring boot
前端开发张小七1 小时前
Java 学习笔记 · 第三课:多线程与并发编程(线程、同步、死锁、Lock、乐观锁与悲观锁)
java·后端·程序员
用户昵称1001 小时前
C/C++编程-工程实践-本地存储log的工程意义
c语言·开发语言
花生了什么事o2 小时前
JVM 垃圾回收:对象如何被判定和回收
java·jvm
evans在进步2 小时前
HashMap 为什么线程不安全?ConcurrentHashMap 如何解决?
java·spring boot·spring
我命由我123452 小时前
匈牙利命名法
java·服务器·后端·学习·java-ee·kotlin·学习方法
闲猫2 小时前
LangChain / Integrations / Integrations by component / Tool
java·数据库·langchain