封装与继承

目录

01:要求设计一个学生类。

02: 编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法进行属性初识化和输出相关数据的方法;使用Test类中的main方法定义各类初始化数据后台打印相关数据

03:请通过代码封装,实现如下需求: 

04:通过继承实现员工工资核算打印功能。

 05:编写一个类Book2,代表教材:

06: 编写一个类Book3,代表教材:

07:根据下图写出“司机”类。(主要体现方法的重载)

08:编写一个类Student,代表学员,要求:

09:宠物类叫Pet(父类) ;

10:父类:家用电器,属性有电流、电压;行为有开open(),关close();子类:电视除还有方法:display();洗衣机还有方法:wash();电灯的特有方法如light();

11:已有Bird类的定义如下:

12:定义一个图形类和(子类)圆形类、矩形类

13:写出this和super的用法。

14:写出static和final的用法

15:编写老师类

16:通过继承实现员工工资核算打印功能。

17:设计父类—员工类(Employee)。子类:工人(Worker)类,农民(Farmer)类,教师(Teacher)类,科学家(Scientist)类,服务生(Waiter)类。

18:定义一个图形类Picture,再定义Picture的两个子类:圆形类Circle、矩形类Rect。

19:为什么要重写toString()方法?

20:为什么要重写equals()方法?

21:定义平面内点的类Point,属性有横坐标和纵坐标。

22:定义一个person类,

23:写出重载与重写的区别。

24:请写出Java基本数据类型及对应的封装类型,并说明封装类的作用。

25:写出“==”和equals的区别。

26:创建一个汽车类(Car)

27:编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。(20分)

28:Personl类、Student类、在Student类里面重写equals方法


01:要求设计一个学生类。

1:属性有:姓名、Java成绩、android成绩、mysql成绩

2:所有属性要求使用private修饰。

3:为每个属性设置setter和getter方法

4:添加有为全部属性赋值的构造方法

5:有输出一个完整学生信息的方法

6:要求可以求学生的总分、平均分、最高分、最低分。

 OneStudent类:

package com.nanchu.extendswork.day18;

public class OneStudent {

    private String name;

    private double javaScore;

    private double androidScore;

    private double mysqlScore;



    public OneStudent() {

    }



    public OneStudent(String name, double javaScore, double androidScore, double mysqlScore) {

        this.name = name;

        this.javaScore = javaScore;

        this.androidScore = androidScore;

        this.mysqlScore = mysqlScore;

    }



    public String getName() {

        return name;

    }



    public void setName(String name) {

        this.name = name;

    }



    public double getJavaScore() {

        return javaScore;

    }



    public void setJavaScore(double javaScore) {

        this.javaScore = javaScore;

    }



    public double getAndroidScore() {

        return androidScore;

    }



    public void setAndroidScore(double androidScore) {

        this.androidScore = androidScore;

    }



    public double getMysqlScore() {

        return mysqlScore;

    }



    public void setMysqlScore(double mysqlScore) {

        this.mysqlScore = mysqlScore;

    }

//5:有输出一个完整学生信息的方法

    @Override

    public String toString() {

        return "{" +

                "姓名='" + this.name + '\'' +

                ", Java成绩=" + this.javaScore +

                ", android成绩=" + this.androidScore +

                ", mysql成绩=" + this.mysqlScore +

                '}';

    }

//    6:要求可以求学生的总分、平均分、最高分、最低分。

    public void studentSum(){

        double sum;

        sum = this.javaScore+this.androidScore+this.mysqlScore;

        System.out.println("总分为:"+sum);

    }

    public void studentAverage(){

        double ave;

        ave = (this.javaScore+this.androidScore+this.mysqlScore)/3;

        System.out.println("平均分为:"+ave);

    }

    public void studentMax(){

        double max;

        max = this.javaScore>this.androidScore?(this.javaScore>this.mysqlScore?this.javaScore:this.mysqlScore):

                (this.androidScore>this.mysqlScore?this.androidScore:this.mysqlScore);

        System.out.println("最高分为:"+max);

    }

    public void studentMin(){

        double min;

        min = this.javaScore

OneTest类:

package com.nanchu.extendswork;



import com.nanchu.extendswork.day18.OneStudent;

import org.junit.jupiter.api.Test;



/**

 * @Author 南初

 * @Create 2024/1/18 18:23

 * @Version 1.0

 */



public class OneTest {

    @Test

    public void student(){

        OneStudent stu = new OneStudent("李四",80,90,88);

        System.out.println(stu.toString());

        stu.studentSum();

        stu.studentAverage();

        stu.studentMax();

        stu.studentMin();

    }



}

封装与继承_第1张图片

02: 编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法进行属性初识化和输出相关数据的方法;使用Test类中的main方法定义各类初始化数据后台打印相关数据

Vehicle类:

package com.nanchu.extendswork.day18.two;

public class Vehicle {

    private int wheels;

    private double weight;



    public Vehicle() {

    }



    public Vehicle(int wheels, double weight) {

        this.wheels = wheels;

        this.weight = weight;

    }



    public int getWheels() {

        return wheels;

    }



    public void setWheels(int wheels) {

        this.wheels = wheels;

    }



    public double getWeight() {

        return weight;

    }



    public void setWeight(double weight) {

        this.weight = weight;

    }



    @Override

    public String toString() {

        return "{" +

                "车轮个数=" + wheels +

                ", 车重=" + weight +

                '}';

    }

}

Car类:

package com.nanchu.extendswork.day18.two;

public class Car extends Vehicle{

    private int loader; // 载人数loader



    public Car() {

    }



    public Car(int wheels, double weight, int loader) {

        super(wheels, weight);

        this.loader = loader;

    }



    public int getLoader() {

        return loader;

    }



    public void setLoader(int loader) {

        this.loader = loader;

    }



    @Override

    public String toString() {

        return "Car{" +

                "车轮个数="+super.getWheels()+

                ", 车重="+super.getWeight()+

                ", 载人数=" + loader +

                '}';

    }

}

Truck类:

package com.nanchu.extendswork.day18.two;

public class Truck extends Car{

    private double payload;  //载重量



    public Truck() {

    }



    public Truck(int wheels, double weight, int loader, double payload) {

        super(wheels, weight, loader);

        this.payload = payload;

    }



    public double getPayload() {

        return payload;

    }



    public void setPayload(double payload) {

        this.payload = payload;

    }



    @Override

    public String toString() {

        return "Truck{" +

                "车轮个数="+super.getWheels()+

                ", 车重="+super.getWeight()+

                ", 载人数=" + super.getLoader() +

                ", 载重量=" + payload +

                '}';

    }

}

TestCar类:

package com.nanchu.extendswork.day18.two;



/**

 * @Author 南初

 * @Create 2024/1/18 19:06

 * @Version 1.0

 */

public class TestCar {

    public static void main(String[] args) {

        Vehicle vh = new Vehicle(4,1000);

        System.out.println(vh.toString());

        vh.setWheels(3);

        vh.setWeight(800);

        System.out.println(vh.toString());

        System.out.println("------------------------");

        Car c=new Car(4,1500,5);

        System.out.println(c.toString());

        c.setWheels(3);

        c.setWeight(1000);

        c.setLoader(4);

        System.out.println(c.toString());

        System.out.println("------------------------");

        Truck truck=new Truck(6,2000,6,3000);

        System.out.println(truck.toString());

        truck.setWheels(4);

        truck.setWeight(1500);

        truck.setLoader(4);

        truck.setPayload(2500);

        System.out.println(truck.toString());



    }

}

03:请通过代码封装,实现如下需求: 

编写一个类Book,代表教材:

        1)具有属性:名称(title)、页数(pageNum),其中页数不能少于200页,否则输出错误信息,并赋予默认值200

        2)每个属性要求使用private修饰,并且为各属性设置赋值和取值方法

        3)具有方法:detail,用来在控制台输出每本教材的名称和页数

        4)编写测试类BookTest进行测试:为Book对象的属性赋予初始值,并调用Book对象的detail方法,看看输出是否正确

Book类:

package com.nanchu.extendswork.day18.three;



//1)具有属性:名称(title)、页数(pageNum),其中页数不能少于200页,

//        否则输出错误信息,并赋予默认值200

//        2)每个属性要求使用private修饰,并且为各属性设置赋值和取值方法

public class Book {

    private String title;

    private int pageNum;



    public Book() {

    }



    public String getTitle() {

        return title;

    }



    public void setTitle(String title) {

        this.title = title;

    }



    public int getPageNum() {

        return pageNum;

    }



    public void setPageNum(int pageNum) {

        if(pageNum>=200){

            this.pageNum = pageNum;

        }else{

            System.out.println("设置错误,页数不能少于200页");

            this.pageNum = 200;

        }

    }

//    3)具有方法:detail,用来在控制台输出每本教材的名称和页数

    public String detail() {

        return "{" +

                "名称='" + title + '\'' +

                ", 页数=" + pageNum +

                '}';

    }



}

BookTest类:

package com.nanchu.extendswork.day18.three;



/**

 * @Author 南初

 * @Create 2024/1/18 19:22

 * @Version 1.0

 */

//4)编写测试类BookTest进行测试:为Book对象的属性赋予初始值,

// 并调用Book对象的detail方法,看看输出是否正确

public class BookTest {

    public static void main(String[] args) {

        Book book = new Book();

        book.setTitle("Java入门");

        book.setPageNum(240);

        System.out.println(book.detail());

        System.out.println("---------------");

        book.setPageNum(180);

        System.out.println(book.detail());

    }

}

封装与继承_第2张图片

04:通过继承实现员工工资核算打印功能。

有员工类,普通员工类和部门经理类,

        普通员工工资=单日工资*天数*等级(1);

        部门经理工资=1000+单日工资*天数*等级(1.2)。

                a)员工属性(单日工资,工作天数,工资等级静态常量),员工方法(打印工资)

                b)普遍员工及部门经理都是员工子类,需要重写打印工资方法。

定义并初始化普通员工对象,调用打印工资方法输入工资,定义并初始化部门经理对象,调用打印工资方法输入工资

package com.nanchu.extendswork.day18.four;



/*

 * 04:通过继承实现员工工资核算打印功能。

 * 有员工类(Employee),普通员工类(Worker)和部门经理类(Manager),

 * 普通员工工资=单日工资*天数*等级(1);

 * 部门经理工资=1000+单日工资*天数*等级(1.2)。

 *        a)员工属性(单日工资,工作天数,工资等级静态常量),员工方法(打印工资)

 *        b)普遍员工及部门经理都是员工子类,需要重写打印工资方法。

 *  定义并初始化普通员工对象,调用打印工资方法输入工资,

 *  定义并初始化部门经理对象,调用打印工资方法输入工资

 * */

public class Employee {

    private double dayWage;

    private int days;

    final static double grade=1;



    public Employee() {

    }



    public Employee(double dayWage, int days) {

        this.dayWage = dayWage;

        this.days = days;

    }



    public double getDayWage() {

        return dayWage;

    }



    public void setDayWage(double dayWage) {

        this.dayWage = dayWage;

    }



    public int getDays() {

        return days;

    }



    public void setDays(int days) {

        this.days = days;

    }



    public void monthSalary(){

    }



}
package com.nanchu.extendswork.day18.four;



public class Manager extends Employee{

    public Manager() {

    }

    public Manager(double dayWage, int days) {

        super(dayWage, days);

    }

// * 普通员工工资=单日工资*天数*等级(1);

    @Override

    public void monthSalary() {

        double money ;

        money = 1000+getDayWage()*getDays()*(grade+0.2);

        System.out.println("部门经理工资:"+money);

    }



}
package com.nanchu.extendswork.day18.four;



public class Worker extends Employee{

    public Worker() {

    }



    public Worker(double dayWage, int days) {

        super(dayWage, days);

    }



    @Override

    public void monthSalary() {

        double money;

        money = getDayWage()*getDays()*grade;

        System.out.println("普通员工工资:"+money);

    }

}
package com.nanchu.extendswork.day18.four;



/**

 * @Author 南初

 * @Create 2024/1/18 20:50

 * @Version 1.0

 */

public class EmployeeTest {

    public static void main(String[] args) {

        Worker wk = new Worker(200,26);

        wk.monthSalary();

        Manager mg = new Manager(300,26);

        mg.monthSalary();

    }

}

封装与继承_第3张图片

 05:编写一个类Book2,代表教材:

        具有属性:名称(title)、页数(pageNum),其中页数不能少于200页,否则输出错误信息,并赋予默认值200

        具有方法:detail,用来在控制台输出每本教材的名称和页数

        具有带参数的构造方法:用来完成对象的初始化工作,并在构造方法中完成对页数的最小值限制

        编写测试类Book2Test进行测试:初始化一个Book2对象,并调用该Book2对象的detail方法,看看输出是否正确

package com.nanchu.extendswork.day18.five;

public class Book2 {

    private String title;

    private int pageNum;



    public Book2() {

    }



    public Book2(String title, int pageNum) {

        this.title = title;

        if(pageNum>=200){

            this.pageNum = pageNum;

        }else{

            System.out.println("设置错误,页数不能少于200页");

            this.pageNum = 200;

        }

    }



    public String getTitle() {

        return title;

    }



    public void setTitle(String title) {

        this.title = title;

    }



    public int getPageNum() {

        return pageNum;

    }



    public void setPageNum(int pageNum) {

        if(pageNum>=200){

            this.pageNum = pageNum;

        }else{

            System.out.println("设置错误,页数不能少于200页");

            this.pageNum = 200;

        }

    }

//    3)具有方法:detail,用来在控制台输出每本教材的名称和页数

    public String detail() {

        return "{" +

                "名称='" + title + '\'' +

                ", 页数=" + pageNum +

                '}';

    }

}
package com.nanchu.extendswork.day18.five;



public class Book2Test {

    public static void main(String[] args) {

        Book2 book2 =new Book2("C语言",240);

        System.out.println(book2.detail());

        System.out.println("-----------------");

        book2 = new Book2("Python",150);

        System.out.println(book2.detail());

    }

}

封装与继承_第4张图片

06 编写一个类Book3,代表教材:

        具有属性:名称(title)、页数(pageNum)、类型(type

        具有方法:detail,用来在控制台输出每本教材的名称、页数、类型

        具有两个带参构造方法:第一个构造方法中,设置教材类型为“计算机”(固定),其余属性的值由参数给定;第二个构造方法中,所有属性的值都由参数给定

        编写测试类Book3Test进行测试:

        分别以两种方式完成对两个Book3对象的初始化工作,并分别调用它们的detail方法,看看输出是否正确

package com.nanchu.extendswork.day18.six;

public class Book3 {

    private String title;

    private int pageNum;

    private String type;



    public Book3() {

    }

//      具有两个带参构造方法:第一个构造方法中,设置教材类型为“计算机”(固定),

//      其余属性的值由参数给定;第二个构造方法中,所有属性的值都由参数给定

    public Book3(int pageNum, String type) {

        this.title = "计算机";

        this.pageNum = pageNum;

        this.type = type;

    }

    public Book3(String title, int pageNum, String type) {

        this.title = title;

        this.pageNum = pageNum;

        this.type = type;

    }



    public String getTitle() {

        return title;

    }



    public void setTitle(String title) {

        this.title = title;

    }



    public int getPageNum() {

        return pageNum;

    }



    public void setPageNum(int pageNum) {

            this.pageNum = pageNum;

    }



    public String getType() {

        return type;

    }



    public void setType(String type) {

        this.type = type;

    }



    //    3)具有方法:detail,用来在控制台输出每本教材的名称和页数

    public String detail() {

        return "{" +

                "名称='" + title + '\'' +

                ", 页数=" + pageNum +

                ", 类型=" + type +

                '}';

    }



}
package com.nanchu.extendswork.day18.six;



public class Book3Test {

    public static void main(String[] args) {

        System.out.println("第一种方式初始化:");

        Book3 book1 = new Book3(300,"工业技术");

        System.out.println(book1.detail());

        System.out.println("第二种方式初始化:");

        Book3 book2 =new Book3("图说中国绘画史",350,"艺术");

        System.out.println(book2.detail());

    }

}

封装与继承_第5张图片

07:根据下图写出“司机”类。(主要体现方法的重载)

package com.nanchu.extendswork.day18.seven;

public class Car {

    //轿车

    public void run(){

        System.out.println("轿车启动行驶");

    }

}
package com.nanchu.extendswork.day18.seven;



public class Bus {

    // 巴士

    public void run(){

        System.out.println("巴士启动");

    }

}
package com.nanchu.extendswork.day18.seven;



public class Train {

    // 火车

    public void run(){

        System.out.println("火车启动行驶");

    }

}
package com.nanchu.extendswork.day18.seven;



public class Driver {

    private String name;



    public Driver() {

    }



    public Driver(String name) {

        this.name = name;

    }



    public void drive(Car c){

        System.out.println(this.name +"开轿车");

        c.run();

    }

    public void drive(Bus b){

        System.out.println(this.name+ "开巴士");

        b.run();

    }

    public void drive(Train t){

        System.out.println(this.name+ "开火车");

        t.run();

    }

}
package com.nanchu.extendswork.day18.seven;



/**

 * @Author 南初

 * @Create 2024/1/19 18:47

 * @Version 1.0

 */

public class Test {

    public static void main(String[] args) {

        Driver driver = new Driver("张三");



        Car c = new Car();

        Bus b = new Bus();

        Train t = new Train();

        driver.drive(c);

        driver.drive(b);

        driver.drive(t);

    }

}

封装与继承_第6张图片

08:编写一个类Student,代表学员,要求:

    (1)具有属性:姓名、年龄、性别、专业。

    (2)具有方法:自我介绍,负责输出该学员的姓名、年龄、性别以

及专业。

    (3)具有两个带参构造方法:第一个构造方法中,设置学员的性别为男、专业为3g,其余属性的值由参数给定;第二个构造方法中,所有属性的值都由参数给定

编写测试类StudentTest进行测试,分别以两种方式完成对两个Student对象的初始化工作,并分别调用它们的自我介绍方法,看看输出是否正确。

package com.nanchu.extendswork.day18.eight;

public class Student {

    // (1)具有属性:姓名、年龄、性别、专业。

    private String name;

    private int age;

    private char sex;

    private String career;

//    (2)具有方法:自我介绍,负责输出该学员的姓名、年龄、性别以及专业。

    public void mySelf(){

        System.out.println("姓名:"+this.name+",年龄:"+this.age+",性别:"+this.age+",专业:"+this.career);

    }

//        (3)具有两个带参构造方法:第一个构造方法中,设置学员的性别为男、专业为3g,

//   其余属性的值由参数给定;第二个构造方法中,所有属性的值都由参数给定3g

    public Student(String name, int age) {

        this.name = name;

        this.age = age;

        this.sex = '男';

        this.career = "3g";

    }



    public Student(String name, int age, char sex, String career) {

        this.name = name;

        this.age = age;

        this.sex = sex;

        this.career = career;

    }

}
package com.nanchu.extendswork.day18.eight;



/**

 * @Author 南初

 * @Create 2024/1/18 20:05

 * @Version 1.0

 */

public class StudentTest {

    public static void main(String[] args) {

        System.out.print("验证第一个构造方法:");

        Student st = new Student("张三",22);

        st.mySelf();

        System.out.print("验证第二个构造方法:");

        Student std = new Student("李子妍",20,'女',"在校学生");

        std.mySelf();

    }

}

封装与继承_第7张图片

09:宠物类叫Pet(父类) ;

属性有name 和 age;  

行为有吃 eat(); 喝 drink(); 叫声shout();

子类 Cat 和 Dog   

Cat继承 Pet类所有并且增加一个自己的特有方法climbTree() 爬树并且叫声是喵喵

Dog继承 Pet类所有并且增加一个自己的特有方法police() 警戒并且叫声是汪汪

package com.nanchu.extendswork.day18.nine;

/*

宠物类叫Pet(父类) ;

属性有name 和 age;

行为有吃 eat(); 喝 drink(); 叫声shout();

 */



public class Pet {

    private String name;

    private int age;



    public Pet() {

    }



    public Pet(String name, int age) {

        this.name = name;

        this.age = age;

    }



    public String getName() {

        return name;

    }



    public void setName(String name) {

        this.name = name;

    }



    public int getAge() {

        return age;

    }



    public void setAge(int age) {

        this.age = age;

    }

    public void eat(){

    }

    public void drink(){

    }

    public void shout(){

    }



    @Override

    public String toString() {

        return "{" +

                "name='" + name + '\'' +

                ", age=" + age +

                '}';

    }

}
package com.nanchu.extendswork.day18.nine;



public class Cat extends Pet{

    public Cat() {

    }



    public Cat(String name, int age) {

        super(name, age);

    }

    public void climbTree(){

        System.out.println(getName()+"会爬树。");

    }



    @Override

    public void shout() {

        System.out.println(getName() +"喵喵叫");

    }



    @Override

    public void eat() {

        System.out.println(getName()+"吃鱼");

    }



    @Override

    public void drink() {

        System.out.println(getName()+"喜欢喝牛奶");

    }

}
package com.nanchu.extendswork.day18.nine;





public class Dog extends Pet{

    public Dog() {

    }



    public Dog(String name, int age) {

        super(name, age);

    }

    public void police(){

        System.out.println("警戒");

    }



    @Override

    public void shout() {

        System.out.println(getName()+"汪汪叫");

    }



    @Override

    public void eat() {

        System.out.println(getName()+"喜欢啃骨头");

    }



    @Override

    public void drink() {

        System.out.println(getName()+"喜欢喝酸奶");

    }

}
package com.nanchu.extendswork.day18.nine;



/**

 * @Author 南初

 * @Create 2024/1/19 13:31

 * @Version 1.0

 */

public class NineTest {

    public static void main(String[] args) {

        Cat cat = new Cat("小白",2);

        System.out.println(cat.toString());

        cat.climbTree();

        cat.shout();

        cat.eat();

        cat.drink();

        Dog dog = new Dog("旺财",3);

        System.out.println(dog.toString());

        dog.police();

        dog.shout();

        dog.eat();

        dog.drink();



    }

}

封装与继承_第8张图片

10:父类:家用电器,属性有电流、电压;行为有开open(),关close();子类:电视除还有方法:display();洗衣机还有方法:wash();电灯的特有方法如light();

package com.nanchu.extendswork.day18.ten;



public class HomeElectric {

    private double electricity; //电流

    private double voltage;     // 电压



    public HomeElectric() {

    }



    public HomeElectric(double electricity, double voltage) {

        this.electricity = electricity;

        this.voltage = voltage;

    }



    public double getElectricity() {

        return electricity;

    }



    public void setElectricity(double electricity) {

        this.electricity = electricity;

    }



    public double getVoltage() {

        return voltage;

    }



    public void setVoltage(double voltage) {

        this.voltage = voltage;

    }



    @Override

    public String toString() {

        return "{" +

                "electricity=" + electricity +

                ", voltage=" + voltage +

                '}';

    }



    public void open(){}

    public void close(){}



}
package com.nanchu.extendswork.day18.ten;



public class TV extends HomeElectric {

    public TV() {

    }



    public TV(double electricity, double voltage) {

        super(electricity, voltage);

    }



    @Override

    public void open() {

        System.out.println("开机");

    }



    @Override

    public void close() {

        System.out.println("关机");

    }

    public void display(){

        System.out.println("电视的功能是播放视频");

    }

}
package com.nanchu.extendswork.day18.ten;



public class Washing extends HomeElectric {

    public Washing() {

    }



    public Washing(double electricity, double voltage) {

        super(electricity, voltage);

    }



    @Override

    public void open() {

        System.out.println("打开洗衣机");

    }



    @Override

    public void close() {

        System.out.println("关闭洗衣机");

    }

    public void wash(){

        System.out.println("洗衣机的功能是洗衣服");

    }



}
package com.nanchu.extendswork.day18.ten;



public class ElectricLamp extends HomeElectric{



    public ElectricLamp() {

    }



    public ElectricLamp(double electricity, double voltage) {

        super(electricity, voltage);

    }



    @Override

    public void open() {

        System.out.println("打开电灯");

    }



    @Override

    public void close() {

        System.out.println("关闭点灯");

    }

    public void light(){

        System.out.println("电灯的功能是照明");

    }

}
package com.nanchu.extendswork.day18.ten;



/**

 * @Author 南初

 * @Create 2024/1/19 13:54

 * @Version 1.0

 */

public class TenTest {

    public static void main(String[] args) {

        TV tv = new TV(1,220);

        System.out.println("电视:"+tv.toString());

        tv.open();

        tv.close();

        tv.display();

        Washing wh = new Washing(2,220);

        System.out.println("洗衣机:"+wh.toString());

        wh.open();

        wh.close();

        wh.wash();

        ElectricLamp el = new ElectricLamp(0.5,220);

        System.out.println("电灯"+el.toString());

        el.open();

        el.close();

        el.light();

    }

}

11:已有Bird类的定义如下:

封装与继承_第9张图片

有子类Nightingale的定义如下,请写出它的输出结果。

封装与继承_第10张图片

分析以上程序的输出结果是什么?为什么?

答案:

Before:0

After:2

12:定义一个图形类和(子类)圆形类、矩形类

图像类:有求周长和求面积和显示图形信息的功能。

圆形类:包含圆心和半径,重写求周长和求面积的方法

矩形类:包含长和宽,重写求周长和求面积的方法

package com.nanchu.extendswork.day18.twelve;



//有求周长和求面积和显示图形信息的功能。

public class Graph {



    public Graph() {

    }

    // 求周长

    public double perimeter(){

        return 0;

    }

    // 求面积

    public double area(){

        return 0;

    }



    public void show(){

        System.out.printf("周长(perimeter):%.2f\t面积(area):%.2f\n",perimeter(),area());

    }

}

package com.nanchu.extendswork.day18.twelve;





public class Round extends Graph{

    private String  center;

    private double radius;



    public Round() {

    }



    public Round(String center, double radius) {

        this.center = center;

        this.radius = radius;

    }

    public double perimeter(){

        return 2*Math.PI*radius;

    }

    // 求面积

    public double area(){

        return Math.PI*Math.pow(radius,2);

    }



    @Override

    public void show() {

        System.out.print("圆心:"+center+",");

        super.show();

    }

}

package com.nanchu.extendswork.day18.twelve;





public class Rectangle extends Graph{

    private double length;

    private double width;



    public Rectangle() {

    }



    public Rectangle(double length, double width) {

        this.length = length;

        this.width = width;

    }

    public double perimeter(){

        return 2*(length+width);

    }

    // 求面积

    public double area(){

        return length*width;

    }

}

package com.nanchu.extendswork.day18.twelve;



/**

 * @Author 南初

 * @Create 2024/1/19 14:38

 * @Version 1.0

 */

public class TwelveTest {

    public static void main(String[] args) {

        Round rd = new Round("(3,3)",2);

        rd.show();

        Rectangle rt = new Rectangle(4,5);

        rt.show();

    }



}

封装与继承_第11张图片

13:写出this和super的用法。

      This:由于Java中成员变量名与局部变量名重复时,成员变量就会失效而不报错,为避免此种情况,就有了this关键字。在成员变量与局部变量相同时,this.a其实就是调用本类的成员变量a;若没有用this,就是a1=a,只是将方法中局部变量a的值赋给a1。而a2=this.a就是将成员变量a的值赋给a2。(调用成员变量)

     Super:1.调用父类的对象,当子类需要调用父类属性时,可以使用super.变量 调用父类;2.调用父类的构造方法,当子类需要使用父类的构造方法时,可以不用重新定义而使用super直接调用父类构造方法。

     Super调用父类的构造方法一般为:

            Public 类名(父类构造方法所有属性,新加属性1,新加属性2,…){

                    Super(按顺序写入父类构造方法中所有变量名用“,”隔开);

                    this.新加属性1 = 新加属性1;

                    this.新加属性2 = 新加属性2;

……

}

在继承父类构造方法的同时也可以添加子类本身属性共同构成构造方法。

14:写出static和final的用法

      final:修饰的成员变量表示常量,修饰的类不能被继承,修饰的方法不能被被重写。

      static:表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态ststic代码块,但是Java语言中没有全局变量的概念。

      注:static final用来修饰成员变量和成员方法,可以简单的理解为“全局常量”。

15:编写老师类

  (1)要求有属性“姓名name”,“年龄age”,“职称post”,“基本工资salary”。

  (2)设置4个静态常量:“部门department”值为“baway”,“工资级别levela ,levelb,levelc”初始值分别为1.1,1.2,1.3。

  (3)编写业务方法, introduce(),实现输出一个教师的信息。

  (4):编写教师类的三个子类:教授类、副教授类、讲师类。工资级别分别为:教授为1.3、副教授为1.2、讲师类1.1。在三个子类里面都重写父类的introduce()方法。

  (5)定义并初始化一个老师对象,调用业务方法,实现对象基本信息的后台打印。

package com.nanchu.extendswork.day18.fifteen;



public class Teacher {

    private String name;

    private int age;

    private String post;  // 职称

    private double salary;



    final static String department ="baway";

    final static double levela =1.1;

    final static double levelb =1.2;

    final static double levelc =1.3;



    public Teacher() {

    }



    public Teacher(String name, int age, String post, double salary) {

        this.name = name;

        this.age = age;

        this.post = post;

        this.salary = salary;

    }



    public String getName() {

        return name;

    }



    public void setName(String name) {

        this.name = name;

    }



    public int getAge() {

        return age;

    }



    public void setAge(int age) {

        this.age = age;

    }



    public String getPost() {

        return post;

    }



    public void setPost(String post) {

        this.post = post;

    }



    public double getSalary() {

        return salary;

    }



    public void setSalary(double salary) {

        this.salary = salary;

    }

    public void introduce(){

    }

}



package com.nanchu.extendswork.day18.fifteen;



public class Professor extends Teacher{

    public Professor() {

    }



    public Professor(String name, int age, String post, double salary) {

        super(name, age, post, salary);

    }

    public void introduce(){

        System.out.println("部门:"+department+

                ", 姓名:"+getName()+", 年龄:"+getAge()+

                ", 职称:"+getPost()+", 基本工资:"+getSalary()+

                ", 工资级别:"+levelc

                );

    }



}

package com.nanchu.extendswork.day18.fifteen;



public class AdjunctProfessor extends Teacher{

    public AdjunctProfessor() {

    }



    public AdjunctProfessor(String name, int age, String post, double salary) {

        super(name, age, post, salary);

    }

    public void introduce(){

        System.out.println("部门:"+department+

                ", 姓名:"+getName()+", 年龄:"+getAge()+

                ", 职称:"+getPost()+", 基本工资:"+getSalary()+

                ", 工资级别:"+levelb

        );

    }

}

package com.nanchu.extendswork.day18.fifteen;





public class Lecturer extends Teacher{

    public Lecturer() {

    }



    public Lecturer(String name, int age, String post, double salary) {

        super(name, age, post, salary);

    }

    public void introduce(){

        System.out.println("部门:"+department+

                ", 姓名:"+getName()+", 年龄:"+getAge()+

                ", 职称:"+getPost()+", 基本工资:"+getSalary()+

                ", 工资级别:"+levela

        );

    }

}

package com.nanchu.extendswork.day18.fifteen;



/**

 * @Author 南初

 * @Create 2024/1/19 15:14

 * @Version 1.0

 */

public class FifteenTest {

    public static void main(String[] args) {

        Professor professor=new Professor("张三",56,"教授",10000);

        professor.introduce();

        AdjunctProfessor adjunctProfessor = new AdjunctProfessor("李四",50,"副教授",8000);

        adjunctProfessor.introduce();

        Lecturer lecturer = new Lecturer("王五",45,"讲师",6000);

        lecturer.introduce();

    }

}

封装与继承_第12张图片

16:通过继承实现员工工资核算打印功能。

父类:员工类employee

子类:部门经理类manager、普通员工类works

部门经理工资=1000+单日工资*天数*等级(1.2)。

普通员工工资=单日工资*天数*等级(1.0);

  1. 员工属性:姓名,单日工资,工作天数
  2. 员工方法(打印工资)
  3. 普遍员工及部门经理都是员工子类,需要重写打印工资方法。(20分) 
  4. 定义并初始化普通员工对象,调用打印工资方法输入工资,定义并初始化部门经理对象,调用打印工资方法输入工资
package com.nanchu.extendswork.day18.sixteen;



public class Employee {

    private String name;

    private double daySalary;

    private double days;

    double[] grade = {1.0,1.2};



    public Employee() {

    }



    public Employee(String name, double daySalary, double days) {

        this.name = name;

        this.daySalary = daySalary;

        this.days = days;

    }



    public String getName() {

        return name;

    }



    public void setName(String name) {

        this.name = name;

    }



    public double getDaySalary() {

        return daySalary;

    }



    public void setDaySalary(double daySalary) {

        this.daySalary = daySalary;

    }



    public double getDays() {

        return days;

    }



    public void setDays(double days) {

        this.days = days;

    }

    public void sumSalary(){



    }

}

package com.nanchu.extendswork.day18.sixteen;



public class Manager extends Employee{

    public Manager() {

    }



    public Manager(String name, double daySalary, double days) {

        super(name, daySalary, days);

    }



    @Override

    public void sumSalary() {

        double sum;

        sum = 1000+getDaySalary()*getDays()*grade[1];

        System.out.println(getName()+"工资为:"+sum);

    }



}

package com.nanchu.extendswork.day18.sixteen;



import com.nanchu.extendswork.day18.fifteen.Teacher;



public class Works extends Employee {

    public Works() {

    }



    public Works(String name, double daySalary, double days) {

        super(name, daySalary, days);

    }



    @Override

    public void sumSalary() {

        double sum;

        sum = getDaySalary()*getDays()*grade[0];

        System.out.println(getName()+"工资为:"+sum);

    }

}

package com.nanchu.extendswork.day18.sixteen;



/**

 * @Author 南初

 * @Create 2024/1/19 15:36

 * @Version 1.0

 */

public class SixteenTest {

    public static void main(String[] args) {

        Manager manager = new Manager("张三",300,26);

        manager.sumSalary();

        Works works = new Works("李四",300,26);

        works.sumSalary();

    }

}

封装与继承_第13张图片

17:设计父类—员工类(Employee)。子类:工人(Worker)类,农民(Farmer)类,教师(Teacher)类,科学家(Scientist)类,服务生(Waiter)类。

  1. 其中工人,农民,服务生只有基本工资。
  2. 教师除基本工资外,还有课酬(元/天)。 
  3. 科学家除基本工资外,还有年终奖。
  4. 编写一个测试类,将各种类型的员工的全年工资打印出来。
package com.nanchu.extendswork.day18.seventeen;



public class Employee {

    private String name;

    private double daySalary;

    private int daySum;



    public Employee() {

    }



    public Employee(String name, double daySalary, int daySum) {

        this.name = name;

        this.daySalary = daySalary;

        this.daySum = daySum;

    }



    public String getName() {

        return name;

    }



    public void setName(String name) {

        this.name = name;

    }



    public double getDaySalary() {

        return daySalary;

    }



    public void setDaySalary(double daySalary) {

        this.daySalary = daySalary;

    }



    public int getDaySum() {

        return daySum;

    }



    public void setDaySum(int daySum) {

        this.daySum = daySum;

    }

    public void yearSalary(){}

}





package com.nanchu.extendswork.day18.seventeen;



public class Worker extends Employee{

    public Worker() {

    }



    public Worker(String name, double daySalary, int daySum) {

        super(name, daySalary, daySum);

    }



    @Override

    public void yearSalary() {

        double summoney;

        summoney = getDaySalary()*getDaySum();

        System.out.println("工人全年工资:"+summoney);

    }

}





package com.nanchu.extendswork.day18.seventeen;



public class Farmer extends Employee{

    public Farmer() {

    }



    public Farmer(String name, double daySalary, int daySum) {

        super(name, daySalary, daySum);

    }

    public void yearSalary() {

        double summoney;

        summoney = getDaySalary()*getDaySum();

        System.out.println("农民全年工资:"+summoney);

    }

}



package com.nanchu.extendswork.day18.seventeen;



public class Waiter extends Employee{

    public Waiter(String name, double daySalary, int daySum) {

        super(name, daySalary, daySum);

    }



    public Waiter() {

    }

    public void yearSalary() {

        double summoney;

        summoney = getDaySalary()*getDaySum();

        System.out.println("服务生全年工资:"+summoney);

    }

}





package com.nanchu.extendswork.day18.seventeen;



public class Teacher extends Employee{

    private double classMoney;



    public Teacher() {

    }



    public Teacher(String name, double daySalary, int daySum, double classMoney) {

        super(name, daySalary, daySum);

        this.classMoney = classMoney;

    }



    public double getClassMoney() {

        return classMoney;

    }



    public void setClassMoney(double classMoney) {

        this.classMoney = classMoney;

    }

    public void yearSalary() {

        double summoney;

        summoney = getDaySalary()*getDaySum()+getDaySum()*getClassMoney();

        System.out.println("教师全年工资:"+summoney);

    }

}





package com.nanchu.extendswork.day18.seventeen;



public class Scientist extends Employee{

    public Scientist() {

    }



    public Scientist(String name, double daySalary, int daySum) {

        super(name, daySalary, daySum);

    }

    public void yearSalary() {

        double sum1,sum2;

        sum1 = getDaySalary()*getDaySum()*0.2;

        sum2 = getDaySum()*getDaySalary();

        System.out.println("年薪:"+sum1+",科学家全年总工资:"+(sum1+sum2));

    }

}



package com.nanchu.extendswork.day18.seventeen;



/**

 * @Author 南初

 * @Create 2024/1/19 16:02

 * @Version 1.0

 */

public class StTest {

    public static void main(String[] args) {

        Worker worker =new Worker("one",300,320);

        worker.yearSalary();

        Farmer farmer = new Farmer("two",180,330);

        farmer.yearSalary();

        Waiter waiter = new Waiter("three",200,330);

        waiter.yearSalary();

        Teacher teacher =new Teacher("four",200,280,10);

        teacher.yearSalary();

        Scientist scientist = new Scientist("five", 400, 300);

        scientist.yearSalary();

    }

}

封装与继承_第14张图片

18:定义一个图形类Picture,再定义Picture的两个子类:圆形类Circle、矩形类Rect。

要求:

        图像类:有求周长和求面积和显示图形信息的功能。

        圆形类:包含圆心和半径,重写求周长和求面积的方法。

        矩形类:包含长和宽,重写求周长和求面积的方法。

package com.nanchu.extendswork.day18.twelve;



public class Picture {



    public Picture() {

    }

    // 求周长

    public double perimeter(){

        return 0;

    }

    // 求面积

    public double area(){

        return 0;

    }



    public void show(){

        System.out.printf("周长:%.2f\t面积:%.2f\n",perimeter(),area());

    }



}

package com.nanchu.extendswork.day18.twelve;





public class Rect extends Picture {

    private double length;

    private double width;



    public Rect() {

    }



    public Rect(double length, double width) {

        this.length = length;

        this.width = width;

    }

    public double perimeter(){

        return 2*(length+width);

    }

    // 求面积

    public double area(){

        return length*width;

    }

}



package com.nanchu.extendswork.day18.twelve;



public class Circle extends Picture {

    private String  center;

    private double radius;



    public Circle() {

    }



    public Circle(String center, double radius) {

        this.center = center;

        this.radius = radius;

    }

    public double perimeter(){

        return 2*Math.PI*radius;

    }

    // 求面积

    public double area(){

        return Math.PI*Math.pow(radius,2);

    }



    @Override

    public void show() {

        System.out.print("圆心:"+center+",");

        super.show();

    }

}



package com.nanchu.extendswork.day18.twelve;



/**

 * @Author 南初

 * @Create 2024/1/19 14:38

 * @Version 1.0

 */

public class TwelveTest {

    public static void main(String[] args) {

        Circle rd = new Circle("(3,3)",3);

        System.out.print("圆的相关信息:");

        rd.show();

        Rect rt = new Rect(2,3);

        System.out.print("举行的相关信息:");

        rt.show();

    }



}

封装与继承_第15张图片

19:为什么要重写toString()方法?

        若不重写toString,输出的则是:包名.类名[email protected]码值,是地址;若重写之后则输出对象本身的值。

20:为什么要重写equals()方法?

  “==”表示比较两个基本数据类型,“equals”表示比较两个对象。若不引入object类的equals方法,则输出为对象的地址,而引入以后则输出对象的值。

21:定义平面内点的类Point,属性有横坐标和纵坐标。

  1. 每个属性都使用private修饰,为每个属性设置settergetter方法。
  2. 设置无参的构造方法
  3. 设置有参的构造方法Pointint xint y
  4. 设置有参的构造方法PointPoint p
  5. 重写toString()方法,用于输出坐标点的信息
  6. 重写euqals()方法,用于对两个坐标点进行比较
package com.nanchu.extendswork.day18.twentyone;



public class Point {

    private int x,y;



    public Point() {

    }



    public Point(int x, int y) {

        this.x = x;

        this.y = y;

    }



    public int getX() {

        return x;

    }



    public void setX(int x) {

        this.x = x;

    }



    public int getY() {

        return y;

    }



    public void setY(int y) {

        this.y = y;

    }



    @Override

    public String toString() {

        return "点坐标(" + x +

                "," + y +

                ")";

    }



    @Override

    public boolean equals(Object o) {

        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        Point point = (Point) o;

        return x == point.x && y == point.y;

    }

}

package com.nanchu.extendswork.day18.twentyone;



/**

 * @Author 南初

 * @Create 2024/1/19 16:36

 * @Version 1.0

 */

public class PointTest {

    public static void main(String[] args) {

        Point point = new Point(3,4);

        System.out.println(point.toString());

        Point p = new Point(1,2);

        System.out.println(p.equals(point));

        Point p1 = new Point(3,4);

        System.out.println(p1.equals(point));

    }

}

封装与继承_第16张图片

22:定义一个person类,

属性如下:

    (1)身份证号,性别,姓名,年龄,户籍,出生日期(Data类型,需要引用Java.uitl.Data

功能:

    (2)自我介绍:介绍格式:(toString

        身份证号+姓名+户籍

    (3)提供对象比较equals方法,只要身份证号+姓名相同就认为对象相等

package com.nanchu.extendswork.day18.twentytwo;



import java.util.Date;

import java.util.Objects;



public class Person {

    private String IDnumber;

    private char sex;

    private String name;

    private int age;

    private String register;  // 户籍

    private Date birthday;



    public Person() {

    }



    public Person(String IDnumber, char sex, String name, int age, String register) {

        this.IDnumber = IDnumber;

        this.sex = sex;

        this.name = name;

        this.age = age;

        this.register = register;

    }



    public String getIDnumber() {

        return IDnumber;

    }



    public void setIDnumber(String IDnumber) {

        this.IDnumber = IDnumber;

    }



    public char getSex() {

        return sex;

    }



    public void setSex(char sex) {

        this.sex = sex;

    }



    public String getName() {

        return name;

    }



    public void setName(String name) {

        this.name = name;

    }



    public int getAge() {

        return age;

    }



    public void setAge(int age) {

        this.age = age;

    }



    public String getRegister() {

        return register;

    }



    public void setRegister(String register) {

        this.register = register;

    }



    public Date getBirthday() {

        return birthday;

    }



    public void setBirthday(Date birthday) {

        this.birthday = birthday;

    }



    @Override

    public String toString() {

        return "我的个人信息如下,{" +

                "身份证号='" + IDnumber + '\'' +

                ", 姓名='" + name + '\'' +

                ", 户籍='" + register + '\'' +

                '}';

    }



    @Override

    public boolean equals(Object o) {

        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        Person person = (Person) o;

        return Objects.equals(IDnumber, person.IDnumber) && Objects.equals(name, person.name);

    }



}

package com.nanchu.extendswork.day18.twentytwo;



/**

 * @Author 南初

 * @Create 2024/1/19 16:56

 * @Version 1.0

 */

public class PersonTest {

    public static void main(String[] args) {

        Person person = new Person("2133446",'男',"张三",26,"安徽");

        System.out.println(person.toString());

        Person p = new Person("2133446",'男',"张三",26,"安徽");

        System.out.println(p.equals(person));

        Person p1 = new Person("2133446",'男',"李四",26,"安徽");

        System.out.println(p1.equals(person));

    }





}

封装与继承_第17张图片

23:写出重载与重写的区别。

        重载:是让类以统一的方式,处理不同类型数据的一种手段。多个同名函数同时存在,具有不同的参数个数/类型。重载的时候,方法名要一样,但参数类型和个数不一样,返回值类型可以相同也可以不同。

        重写:是当父类的方法不能满足子类需求的时候,子类需要重写这个方法达到所需的功能。

        方法的重载发生在同一个类中,方法的从写发生在不同的类中。

24:请写出Java基本数据类型及对应的封装类型,并说明封装类的作用。

byte----> Byte

short----> Short

int---->Integer

long----> Long

double----> Double

float----> Float

boolean----> Boolean

char----> Characater

25:写出“==”和equals的区别。

   “==”比较的是基本数据类型;比较的是值。

   “equals”比较的是引用数据类型;比较的地址。

26:创建一个汽车类(Car)

  1. 为其定义两个属性:颜色和型号。每个属性都使用private进行封装,为每个属性设置set、get方法。
  2. 为该类创建两个构造方法。第一个为无参的构造方法。第二个为带参构造方法
  3. 重写toString方法,通过toString输出汽车信息。
  4. 重写equals方法,如果汽车的颜色和型号相同就认为是同一辆车。
  5. 实例化两个对象,输出两个对象的信息,比较两个对象是否是同一个对象。
package com.nanchu.extendswork.day18.twentysix;



import java.util.Objects;



public class Car {

    private String color;

    private String model;



    public Car() {

    }



    public Car(String color, String model) {

        this.color = color;

        this.model = model;

    }



    public String getColor() {

        return color;

    }



    public void setColor(String color) {

        this.color = color;

    }



    public String getModel() {

        return model;

    }



    public void setModel(String model) {

        this.model = model;

    }



    @Override

    public String toString() {

        return "{" +

                "颜色='" + color + '\'' +

                ", 型号='" + model + '\'' +

                '}';

    }



    @Override

    public boolean equals(Object o) {

        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        Car car = (Car) o;

        return Objects.equals(color, car.color) && Objects.equals(model, car.model);

    }



}

package com.nanchu.extendswork.day18.twentysix;



/**

 * @Author 南初

 * @Create 2024/1/19 17:12

 * @Version 1.0

 */

public class CarTest {

    public static void main(String[] args) {

        Car c1 = new Car("白色","A");

        Car c2 = new Car("黑色", "A");

        Car c3 = new Car("白色", "A");

        System.out.println(c1.toString());

        System.out.println(c2.toString());

        System.out.println(c3.toString());

        System.out.println("c1和c2是否是同一个对象:"+c1.equals(c2));

        System.out.println("c2和c3是否是同一个对象:"+c2.equals(c3));

        System.out.println("c1和c3是否是同一个对象:"+c1.equals(c3));



    }

}

封装与继承_第18张图片

27:编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。(20分)

  1. 每个类都有构造方法进行属性初识化
  2. 每个类都输出相关数据的toString方法
  3. 使用Test类中的main方法定义各类初始化数据后台打印相关数据
package com.nanchu.extendswork.day18.twentyseven;



public class Vehicle {

    private int wheels;

    private double weight;



    public Vehicle() {

    }



    public Vehicle(int wheels, double weight) {

        this.wheels = wheels;

        this.weight = weight;

    }



    public int getWheels() {

        return wheels;

    }



    public void setWheels(int wheels) {

        this.wheels = wheels;

    }



    public double getWeight() {

        return weight;

    }



    public void setWeight(double weight) {

        this.weight = weight;

    }



    @Override

    public String toString() {

        return "{" +

                "车轮个数=" + wheels +

                ", 车重=" + weight +

                '}';

    }

}

package com.nanchu.extendswork.day18.twentyseven;





public class Truck extends Car {

    private double payload;  //载重量



    public Truck() {

    }



    public Truck(int wheels, double weight, int loader, double payload) {

        super(wheels, weight, loader);

        this.payload = payload;

    }



    public double getPayload() {

        return payload;

    }



    public void setPayload(double payload) {

        this.payload = payload;

    }



    @Override

    public String toString() {

        return "Truck{" +

                "车轮个数="+super.getWheels()+

                ", 车重="+super.getWeight()+

                ", 载人数=" + super.getLoader() +

                ", 载重量=" + payload +

                '}';

    }

}

package com.nanchu.extendswork.day18.twentyseven;





public class Car extends Vehicle {

    private int loader; // 载人数loader



    public Car() {

    }



    public Car(int wheels, double weight, int loader) {

        super(wheels, weight);

        this.loader = loader;

    }



    public int getLoader() {

        return loader;

    }



    public void setLoader(int loader) {

        this.loader = loader;

    }



    @Override

    public String toString() {

        return "Car{" +

                "车轮个数="+super.getWheels()+

                ", 车重="+super.getWeight()+

                ", 载人数=" + loader +

                '}';

    }

}



package com.nanchu.extendswork.day18.twentyseven;



public class Vehicle {

    private int wheels;

    private double weight;



    public Vehicle() {

    }



    public Vehicle(int wheels, double weight) {

        this.wheels = wheels;

        this.weight = weight;

    }



    public int getWheels() {

        return wheels;

    }



    public void setWheels(int wheels) {

        this.wheels = wheels;

    }



    public double getWeight() {

        return weight;

    }



    public void setWeight(double weight) {

        this.weight = weight;

    }



    @Override

    public String toString() {

        return "{" +

                "车轮个数=" + wheels +

                ", 车重=" + weight +

                '}';

    }

}

28:Personl类、Student类、在Student类里面重写equals方法

        (1)编写一个人类Person,其中包含姓名、性别和年龄的属性,包含构造方法以及显示姓名、性别和年龄的方法。
        (2)再编写一个学生类Student,它继承Person类,其中包含学号属性,包含构造方法以及显示学号的方法。
        (3)在Student类里面重写equals方法,只要是学号和姓名一致就认为是同一个学生。
        (4)最后编写一个测试类,此类里面包含main()方法,在main()方法中定义两个学生s1和s2并给他们赋值,最后显示他们的学号、姓名、性别以及年龄,以及判断两个学生是否是同一个学生。
package com.nanchu.extendswork.day18.twentyeight;



public class Person {

    private String name;

    private char sex;

    private int age;



    public Person() {

    }

    public Person(String name, char sex, int age) {

        this.name = name;

        this.sex = sex;

        this.age = age;

    }



    public String getName() {

        return name;

    }



    public void setName(String name) {

        this.name = name;

    }



    public char getSex() {

        return sex;

    }



    public void setSex(char sex) {

        this.sex = sex;

    }



    public int getAge() {

        return age;

    }



    public void setAge(int age) {

        this.age = age;

    }



    @Override

    public String toString() {

        return "{" +

                "name='" + name + '\'' +

                ", sex=" + sex +

                ", age=" + age +

                '}';

    }

}

package com.nanchu.extendswork.day18.twentyeight;



import java.util.Objects;



public class Student extends Person{

    private int stuID;



    public Student() {

    }



    public Student(String name, char sex, int age, int stuID) {

        super(name, sex, age);

        this.stuID = stuID;

    }



    public int getStuID() {

        return stuID;

    }



    public void setStuID(int stuID) {

        this.stuID = stuID;

    }



    @Override

    public String toString() {

        return "{" +

                "name='" + getName() + '\'' +

                ", sex=" + getSex() +

                ", age=" + getAge() +

                ", stuID=" + stuID +

                '}';

    }



    @Override

    public boolean equals(Object o) {

        if (this == o) return true;

        if (!(o instanceof Student)) return false;

        Student student = (Student) o;

        return getStuID() == student.getStuID()&& Objects.equals(getName(), student.getName());

    }



}



package com.nanchu.extendswork.day18.twentyeight;



/**

 * @Author 南初

 * @Create 2024/1/19 18:14

 * @Version 1.0

 */

public class PersonTest {

    public static void main(String[] args) {

        Student s1= new Student("张三",'男',22,1234);

        Student s2= new Student("张三",'男',22,1234);

        System.out.println(s1.toString());

        System.out.println(s2.toString());

        System.out.println("两个学生是否是同一个学生:"+s1.equals(s2));

    }

}

封装与继承_第19张图片

你可能感兴趣的:(java练习集合,java,javaee)