Java2EE基础练习及面试题_chapter05面向对象(中_01)

简介: Java2EE基础练习及面试题_chapter05面向对象(中_01)

题目01

571d5e79021d4107ab9d34a637a759c1.png

package com.jerry.java;
/**
 * @author jerry_jy
 * @create 2022-09-28 9:24
 */
//Student类继承Person类
public class Exer1 {
    public static void main(String[] args) {
        Person person = new Person("tom", 'M', 21);
        System.out.println(person.toString());
        Student student = new Student(10001L, 98, 99, 97);
        System.out.println("平均分:" + student.aver());
        System.out.println("最大分:" + student.max());
        System.out.println("最小分:" + student.min());
        System.out.println(student.toString());
    }
}
class Student extends Person {
    Long number;
    int math;
    int english;
    int computer;
    public Student() {
    }
    public Student(Long number, int math, int english, int computer) {
        this.number = number;
        this.math = math;
        this.english = english;
        this.computer = computer;
    }
    public Student(String name, char sex, int age, Long number, int math, int english, int computer) {
        super(name, sex, age);
        this.number = number;
        this.math = math;
        this.english = english;
        this.computer = computer;
    }
    public double aver() {
        double avg = (math + english + computer) / 3;
        return avg;
    }
    public int max() {
        int max = (math > english) ? (math > computer ? math : computer) : (english > computer ? english : computer);
        return max;
    }
    public int min() {
        int min = (math < english) ? (math < computer ? math : computer) : (english < computer ? english : computer);
        return min;
    }
    @Override
    public String toString() {
        return "Student{" +
                "number=" + number +
                ", math=" + math +
                ", english=" + english +
                ", computer=" + computer +
                ", name='" + name + '\'' +
                ", sex=" + sex +
                ", age=" + age +
                '}';
    }
}
class Person {
    String name;
    char sex;
    int age;
    public Person() {
    }
    public Person(String name, char sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", sex=" + sex +
                ", age=" + age +
                '}';
    }
}

题目02

/*
(1)定义一个ManKind类,包括
    成员变量int sex和int salary;
    方法void manOrWoman():根据sex的值显示“man”(sex==1)或者“woman”(sex==0);
    方法void employeed():根据salary的值显示“no job”(salary==0)或者“ job”(salary!=0)。
(2)定义类Kids继承ManKind,并包括
    成员变量int yearsOld;
    方法printAge()打印yearsOld的值。
(3)定义类KidsTest,在类的main方法中实例化Kids的对象someKid,用该对象访问其父类的成员变量及方法。
 */
package com.jerry.java;
/**
 * @author jerry_jy
 * @create 2022-09-28 9:40
 */
public class Exer2 {
    /*
    (1)定义一个ManKind类,包括
        成员变量int sex和int salary;
        方法void manOrWoman():根据sex的值显示“man”(sex==1)或者“woman”(sex==0);
        方法void employeed():根据salary的值显示“no job”(salary==0)或者“ job”(salary!=0)。
    (2)定义类Kids继承ManKind,并包括
        成员变量int yearsOld;
        方法printAge()打印yearsOld的值。
    (3)定义类KidsTest,在类的main方法中实例化Kids的对象someKid,用该对象访问其父类的成员变量及方法。
     */
    public static void main(String[] args) {
        Kids someKid = new Kids(1,0,13);
        someKid.manOrWoman();
        someKid.employed();
        someKid.printOld();
    }
}
class Kids extends ManKind {
    int yearsOld;
    public Kids() {
    }
    public Kids(int yearsOld) {
        this.yearsOld = yearsOld;
    }
    public Kids(int sex, int salary, int yearsOld) {
        super(sex, salary);
        this.yearsOld = yearsOld;
    }
    public Kids(int sex, int salary) {
        super(sex, salary);
    }
    public void printOld() {
        System.out.println("我的年龄是:" + yearsOld);
    }
    public void employed() {
        super.employed();//调用父类的方法
        System.out.println("Kids should study and no job");
    }
}
class ManKind {
    int sex;
    int salary;
    public ManKind() {
    }
    public ManKind(int sex, int salary) {
        this.sex = sex;
        this.salary = salary;
    }
    public void manOrWoman() {
        if (sex == 1) {
            System.out.println("man");
        }
        if (sex == 0) {
            System.out.println("woman");
        }
    }
    public void employed() {
        if (salary == 0) {
            System.out.println("no job!");
        } else {
            System.out.println("job!");
        }
    }
}

题目03

eef8b7ce3b4848778ecc638b8eb31006.png

package com.jerry.java;
/**
 * @author jerry_jy
 * @create 2022-09-28 9:55
 */
public class Exer3 {
    /*
    1.修改练习1.2中定义的类Kids中employeed()方法,在该方法中调用父类
    ManKind的employeed()方法,然后再输出“but Kids should study and no job.”
    2.修改练习1.3中定义的Cylinder类,在Cylinder类中覆盖findArea()方法,计算圆柱的表面积。考虑:findVolume方法怎样做相应的修改?
    在CylinderTest类中创建Cylinder类的对象,设置圆柱的底面半径和高,并输出    圆柱的表面积和体积。
    附加题:在CylinderTest类中创建一个Circle类的对象,设置圆的半径,计算输出圆的面积。体会父类和子类成员的分别调用。
    写一个测试类,创建两个Circle对象,判断其颜色是否相等;利用equals方法判断其半径是否相等;利用
toString()方法输出其半径。
     */
    public static void main(String[] args) {
        Circle circle = new Circle();
        System.out.println(circle.getRadius());//获取默认值
        System.out.println(circle.findArea());//默认面积
        System.out.println("===================");
        circle.setRadius(2.0);
        System.out.println(circle.getRadius());//设置新的值
        System.out.println(circle.findArea());//获取新的面积
        System.out.println("===================");
        Cylinder cylinder = new Cylinder();
        System.out.println(cylinder.getLength());
        System.out.println(cylinder.getRadius());
        System.out.println(cylinder.findArea());
        System.out.println(cylinder.finaVolume());
        System.out.println("===================");
        cylinder.setLength(2.0);
        cylinder.setRadius(2.0);//子类调用父类
        System.out.println(cylinder.getLength());
        System.out.println(cylinder.getRadius());
        System.out.println(cylinder.findArea());
        System.out.println(cylinder.finaVolume());
        Circle cylinder1 = new Cylinder();
    }
}
class Cylinder extends Circle {
    private double length;
    public Cylinder() {
        this.length = 1.0;// 构造器赋值
    }
    public double getLength() {
        return length;
    }
    public void setLength(double length) {
        this.length = length;
    }
    public double findArea() {
        double area = super.findArea() * 2 + 2 * Math.PI * super.getRadius() * length;//获取表面积
        return area;
    }
    public double finaVolume() {
        double volume = super.findArea() * length;
        return volume;
    }
}
class Circle {
    private double radius;
    public Circle() {
        this.radius = 1.0; // 构造器赋值
    }
    public double getRadius() {
        return radius;
    }
    public void setRadius(double radius) {
        this.radius = radius;
    }
    public double findArea() {
        double area = Math.PI * radius * radius;
        return area;
    }
}

题目04

//练习:继承成员变量和继承方法的区别
package com.jerry.java;
/**
 * @author jerry_jy
 * @create 2022-09-28 10:15
 */
public class Exer4 {
    //练习:继承成员变量和继承方法的区别
    public static void main(String[] args) {
        Sub s = new Sub();
        System.out.println(s.count);//20
        s.display();//20
        Base b = s;
        System.out.println(b == s);//true
        System.out.println(b.count);//10
        b.display();//20
    }
}
class Base {
    int count = 10;
    public void display() {
        System.out.println(this.count);
    }
}
class Sub extends Base {
    int count = 20;
    public void display() {
        System.out.println(this.count);
    }
}

题目05

59188860b43243a8be4f51111cf76ce7.png

package com.jerry.java;
/**
 * @author jerry_jy
 * @create 2022-09-28 16:24
 */
public class Exer5 {
    /*
    定义三个类,父类GeometricObject代表几何形状,子类Circle代表圆形,MyRectangle代表矩形。
    定义一个测试类GeometricTest,编写equalsArea方法测试两个对象的面积是否相等(注意方法的参
    数类型,利用动态绑定技术),编写displayGeometricObject方法显示对象的面积(注意方法的参
    数类型,利用动态绑定技术)
     */
    public boolean equalsArea(GeometricObject object, GeometricObject object1) {
        return object == object1;
    }
    public double displayGeometricObject(GeometricObject object) {
        return object.findArea();
    }
    public static void main(String[] args) {
        GeometricObject geometricObject = new GeometricObject("white", 3.0);
        MyCircle myCircle = new MyCircle(2.0);
//        System.out.println(myCircle.getColor());
//        System.out.println(myCircle.getWeight());
        System.out.println(myCircle.getRadius());
        System.out.println(myCircle.findArea());
        System.out.println("==========================");
        MyRectangle myRectangle = new MyRectangle(1.0, 2.0);
        Exer5 exer5 = new Exer5();
        System.out.println(exer5.equalsArea(myCircle, myRectangle));
        System.out.println(exer5.displayGeometricObject(myRectangle));
        System.out.println("=========================");
        MyCircle circle1 = new MyCircle("white", 1.0, 1.0);
        MyCircle circle2 = new MyCircle("white", 1.0, 3.0);
        System.out.println(circle1.equals(circle2));
        System.out.println(circle1.toString());
    }
}
class GeometricObject {
    String color;
    double weight;
    public GeometricObject() {
    }
    public GeometricObject(String color, double weight) {
        this.color = color;
        this.weight = weight;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public double getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
    public double findArea() {
        double area = 0.0;
        return area;
    }
}
class MyCircle extends GeometricObject {
    private double radius;
    public MyCircle() {
    }
    public MyCircle(double radius) {
        this.radius = radius;
    }
    public MyCircle(String color, double weight, double radius) {
        super(color, weight);
        this.radius = radius;
    }
    public double getRadius() {
        return radius;
    }
    public void setRadius(double radius) {
        this.radius = radius;
    }
    public double findArea() {
        double area = Math.PI * radius * radius;
        return area;
    }
    public boolean equals(MyCircle obj) {
        return this.radius == obj.radius;
    }
    public String toString() {
        String str = "圆的半径:" + radius;
        return str;
    }
}
class MyRectangle extends GeometricObject {
    private double width;
    private double height;
    public MyRectangle() {
    }
    public MyRectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }
    public MyRectangle(String color, double weight, double width, double height) {
        super(color, weight);
        this.width = width;
        this.height = height;
    }
    public double getWidth() {
        return width;
    }
    public void setWidth(double width) {
        this.width = width;
    }
    public double getHeight() {
        return height;
    }
    public void setHeight(double height) {
        this.height = height;
    }
    public double findArea() {
        double area = height * width;
        return area;
    }
}

题目06

    /*
    编写Order类,有int型的orderId,String型的orderName,相应的
getter()和setter()方法,两个参数的构造器,重写父类的equals()方法:
public boolean equals(Object obj),并判断测试类中创建的两个对象是否
相等。
     */
package com.jerry.java;
import com.sun.org.apache.xpath.internal.operations.Or;
/**
 * @author jerry_jy
 * @create 2022-09-28 17:13
 */
public class Exer8 {
    public static void main(String[] args) {
        Order order = new Order();
        Order order1 = new Order();
        System.out.println(order.equals(order1));
    }
}
class Order{
    int orderId;
    String orderName;
    public Order() {
    }
    public Order(int orderId, String orderName) {
        this.orderId = orderId;
        this.orderName = orderName;
    }
    public int getOrderId() {
        return orderId;
    }
    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }
    public String getOrderName() {
        return orderName;
    }
    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }
    public boolean equals(Object obj){
        return this.getClass()==obj;
    }
}

题目07

    /*
    请根据以下代码自行定义能满足需要的MyDate类,在MyDate类中覆盖
equals方法,使其判断当两个MyDate类型对象的年月日都相同时,结果
为true,否则为false。 public boolean equals(Object o)
     */
package com.jerry.java;
import java.util.Date;
/**
 * @author jerry_jy
 * @create 2022-09-28 17:21
 */
public class Exer9 {
    /*
    请根据以下代码自行定义能满足需要的MyDate类,在MyDate类中覆盖
equals方法,使其判断当两个MyDate类型对象的年月日都相同时,结果
为true,否则为false。 public boolean equals(Object o)
     */
    public static void main(String[] args) {
        MyDate myDate = new MyDate(2022, 01, 01);
        MyDate myDate1 = new MyDate(2020, 01, 01);
        System.out.println(myDate1.equals(myDate1));
    }
}
class MyDate {
    int year, month, day;
    public MyDate() {
    }
    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
    public int getMonth() {
        return month;
    }
    public void setMonth(int month) {
        this.month = month;
    }
    public int getDay() {
        return day;
    }
    public void setDay(int day) {
        this.day = day;
    }
    public boolean equals(MyDate o){
        return this.year == o.year && this.month == o.month && this.day == o.day;
    }
}

题目08

    // 包装类的使用(相互转化)
        int i = 5;
        Integer integer = new Integer(i);//把基本数据类型转化为包装类,调用new Integer()
        System.out.println(integer.getClass().getName());//java.lang.Integer
        int i1 = integer.intValue();//包装类转化为基本数据类型,调用包装类的方法:xxxValue()
        System.out.println(i1);//5
        int j = 1;
        String s = String.valueOf(j);// 基本数据类型转化为String类,方式一:调用String.valueOf()
        System.out.println(s.getClass().getName());//java.lang.String
        String s1 = j + ""; // 基本数据类型转化为String类,方式二:+ ""
        System.out.println(s1.getClass().getName());//java.lang.String
        String s2 = "123";
        int i2 = Integer.parseInt(s2); // String类转化为基本数据类型,调用Integer.parseInt()
        System.out.println(i2);//123
        Integer integer1 = new Integer(2);
        String s3 = integer1.toString();// 包装类转化为String类,调用包装类对象的toString()方法
        System.out.println(s3.getClass().getName());//java.lang.String
        Integer i3 = new Integer("456");// String类转化为包装类
        System.out.println(i3.getClass().getName()); //java.lang.Integer

题目09

/*
利用Vector代替数组处理:从键盘读入学生成绩(以负数代表输入结束),找出最高分,并输出学生成绩等级。
提示:数组一旦创建,长度就固定不变,所以在创建数组前就需要知道它的
长度。而向量类java.util.Vector可以根据需要动态伸缩。
创建Vector对象:Vector v=new Vector();
给向量添加元素:v.addElement(Object obj); //obj必须是对象
取出向量中的元素:Object obj=v.elementAt(0);
注意第一个元素的下标是0,返回值是Object类型的。
计算向量的长度:v.size();
若与最高分相差10分内:A等;20分内:B等;
30分内:C等;其它:D等
 */
package com.jerry.java;
import java.util.Scanner;
import java.util.Vector;
/**
 * @author jerry_jy
 * @create 2022-09-28 19:52
 */
public class Exer11 {
    public static void main(String[] args) {
        System.out.println("从键盘读入学生成绩(以负数代表输入结束):");
        Vector v=new Vector();
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            Scanner scanner = new Scanner(System.in);
            int score = scanner.nextInt();
            Integer obj = new Integer(score);
            v.addElement(obj);
            if (score<0){
                break;
            }
        }
        int size = v.size();
        System.out.println("size: "+size);
        System.out.println(v.toString());
    }
}

题目10

//写出下面程序的运行结果
    public static void main(String[] args) {
        A a = new A();
        a.show();
        A b=new B();
        b.show();
        String str1 = "abc";
        String str2 = "abc";
        System.out.println(str1.equals(str2));//true,equals()是比较字符串的值,
        if (str1==str2){
            System.out.println("true");//true,==是比较地址值,但是由于字符串存在于常量池中,两个一模一样内容的字符串常量池中只会保存一份,指向相同的地址值
        }else {
            System.out.println("false");
        }
    }
}
class A {
    int a = 1;
    double d = 2.0;
    void show() {
        System.out.println("Class A: a=" + a + "\td=" + d);
    }
}
class B extends A {
    float a = 3.0f;
    String d = "Java program.";
    void show() {
        super.show();
        System.out.println("Class B: a=" + a + "\td=" + d);
    }
}

题目11

/*
按要求实现下列问题:实现一个名为Person的类和它的子类Employee,Employee有两个子类Faculty和Staff。具体要求如下:
1)  Person类中的属性有:姓名name(String类型),地址address(String类型),
电话号码telphone(String类型)和电子邮件地址email(String类型);
2)  Employee类中的属性有:办公室office(String类型),工资wage(double
类型),受雇日期hiredate(String类型);
3)  Faculty类中的属性有:学位degree(String类型),级别level(String类型);
4)  Staff类中的属性有:职务称号duty(String类型)。
5)  现有对象Person p1 =new Faculty()和Person p2 =new Staff (),请分别为p1的属性赋值“本科”和Staff类的duty赋值“职员”
 */
package com.jerry.exer;
/**
 * @author jerry_jy
 * @create 2022-09-28 20:47
 */
public class Exer2 {
    public static void main(String[] args) {
        //方法声明的形参类型为父类类型,可以使用子类的对象作为实参调用该方法
        Person p1 = new Faculty();
        Person p2 = new Staff();
        p1.setDegree(new Faculty("本科"));
        p2.setDuty(new Staff("职员"));
    }
}
class Person {
    String name, address, telephone, email;
    public void setDegree(Faculty faculty) {
    }
    public void setDuty(Staff staff) {
    }
}
class Employee extends Person {
    String office;
    double wage;
    String hireDate;
}
class Faculty extends Employee {
    String degree;
    String level;
    public Faculty() {
    }
    public Faculty(String degree) {
        this.degree = degree;
    }
    public String getDegree() {
        return degree;
    }
    public void setDegree(String degree) {
        this.degree = degree;
    }
}
class Staff extends Employee {
    String duty;
    public Staff() {
    }
    public Staff(String duty) {
        this.duty = duty;
    }
    public String getDuty() {
        return duty;
    }
    public void setDuty(String duty) {
        this.duty = duty;
    }
}

题目12

/*
定义一个person类,属性如下:
(1)身份证号,性别,姓名,年龄,户籍,出生日期(Data类型,需要引用java.uitl.Data)功能:
(2)自我介绍:介绍格式:(toString)
身份证号+姓名+户籍
(3)提供对象比较equals方法,只要身份证号+姓名相同就认为对象相等
 */
package com.jerry.exer;
import java.util.Date;
/**
 * @author jerry_jy
 * @create 2022-09-28 21:24
 */
public class Exer3 {
    /*
    定义一个person类,属性如下:
    (1)身份证号,性别,姓名,年龄,户籍,出生日期(Data类型,需要引用java.uitl.Data)功能:
    (2)自我介绍:介绍格式:(toString)
    身份证号+姓名+户籍
    (3)提供对象比较equals方法,只要身份证号+姓名相同就认为对象相等
     */
    public static void main(String[] args) {
        Person1 person1 = new Person1("123", 'M', "Zhang3", 20, "456", new Date());
        System.out.println(person1.toString());
        Person1 person2 = new Person1("1234", 'F', "Zhang3", 21, "789", new Date());
        System.out.println(person2.equals(person1));
    }
}
class Person1 {
    String IDCard;
    char gender;
    String name;
    int age;
    String address;
    Date birth;
    public Person1() {
    }
    public Person1(String IDCard, char gender, String name, int age, String address, Date birth) {
        this.IDCard = IDCard;
        this.gender = gender;
        this.name = name;
        this.age = age;
        this.address = address;
        this.birth = birth;
    }
    public String toString() {
        String str = "身份证号: " + IDCard + ",姓名: " + name + ",户籍: " + address;
        return str;
    }
    public boolean equals(Person1 person1) {
        if (this.IDCard == person1.IDCard && this.name == person1.name) {
            return true;
        } else {
            return false;
        }
    }
}

题目13

/*
创建一个汽车类(Car)
(1)为其定义两个属性:颜色和型号。每个属性都使用private进行封装,为每个属性设置set、get方法。
(2)为该类创建两个构造方法。第一个为无参的构造方法。第二个为带参构造方法
(3)重写toString方法,通过toString输出汽车信息。
(4)重写equals方法,如果汽车的颜色和型号相同就认为是同一辆车。
(5)实例化两个对象,输出两个对象的信息,比较两个对象是否是同一个对象。
 */
package com.jerry.exer;
/**
 * @author jerry_jy
 * @create 2022-09-28 21:37
 */
public class Exer4 {
    /*
    创建一个汽车类(Car)
    (1)为其定义两个属性:颜色和型号。每个属性都使用private进行封装,为每个属性设置set、get方法。
    (2)为该类创建两个构造方法。第一个为无参的构造方法。第二个为带参构造方法
    (3)重写toString方法,通过toString输出汽车信息。
    (4)重写equals方法,如果汽车的颜色和型号相同就认为是同一辆车。
    (5)实例化两个对象,输出两个对象的信息,比较两个对象是否是同一个对象。
     */
    public static void main(String[] args) {
        Car car1 = new Car("white", "BMW");
        System.out.println(car1.toString());
        car1.setColor("black");
        car1.setBrand("奔驰");
        System.out.println(car1.toString());
        Car car2 = new Car("black", "奔驰");
        System.out.println(car2.toString());
        System.out.println(car2.equals(car1));
    }
}
class Car {
    private String color;
    private String brand;
    public Car() {
    }
    public Car(String color, String brand) {
        this.color = color;
        this.brand = brand;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    @Override
    public String toString() {
        return "Car{" +
                "color='" + color + '\'' +
                ", brand='" + brand + '\'' +
                '}';
    }
    public boolean equals(Car car) {
        if (this.color == car.color && this.brand == car.brand) {
            return true;
        } else {
            return false;
        }
    }
}

题目14

/*
编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。
(1)每个类都有构造方法进行属性初识化
(2)每个类都输出相关数据的toString方法
(3)使用Test类中的main方法定义各类初始化数据后台打印相关数据
 */
package com.jerry.exer;
/**
 * @author jerry_jy
 * @create 2022-09-28 21:45
 */
public class Exer5 {
    /*
    编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。
    (1)每个类都有构造方法进行属性初识化
    (2)每个类都输出相关数据的toString方法
    (3)使用Test类中的main方法定义各类初始化数据后台打印相关数据
     */
    public static void main(String[] args) {
        Vehicle vehicle = new Vehicle(4, 2000.0);
        System.out.println(vehicle.toString());
        Car1 car1 = new Car1(vehicle, 5);//把父类对象传进来
        System.out.println(car1.toString());
        Truck truck = new Truck(car1, 3500.0);//把父类对象传进来
        System.out.println(truck.toString());
    }
}
class Vehicle {
    int wheels;
    double weight;
    public Vehicle() {
    }
    public Vehicle(int wheels, double weight) {
        this.wheels = wheels;
        this.weight = weight;
    }
    @Override
    public String toString() {
        return "Vehicle{" +
                "wheels=" + wheels +
                ", weight=" + weight +
                '}';
    }
}
class Car1 extends Vehicle {
    int loader;//载人数
    public Car1() {
    }
    public Car1(int loader) {
        this.loader = loader;
    }
    public Car1(Vehicle vehicle, int loader) {//把父类对象传进来,免得又重新初始化
        super(vehicle.wheels, vehicle.weight);
        this.loader = loader;
    }
    public Car1(int wheels, double weight, int loader) {
        super(wheels, weight);
        this.loader = loader;
    }
    @Override
    public String toString() {
        return "Car1{" +
                "wheels=" + wheels +
                ", weight=" + weight +
                ", loader=" + loader +
                '}';
    }
}
class Truck extends Car1 {
    double payload;//载重
    public Truck() {
    }
    public Truck(double payload) {
        this.payload = payload;
    }
    public Truck(Car1 car1, double payload) {
        super(car1.wheels, car1.weight, car1.loader);
        this.payload = payload;
    }
    public Truck(int wheels, double weight, int loader, double payload) {
        super(wheels, weight, loader);
        this.payload = payload;
    }
    @Override
    public String toString() {
        return "Truck{" +
                "wheels=" + wheels +
                ", weight=" + weight +
                ", loader=" + loader +
                ", payload=" + payload +
                '}';
    }
}

题目15

/*
定义员工类Employee,包含姓名、工号和工资,包含计算奖金方法bonus,普通员工和经理都是员工,计算奖金的方法为工资*奖金系数,普通员工的奖金系数为1.5(常量),经理为2(常量),分别实现bonus方法,创建对象测试。
 */
package com.jerry.exer;
/**
 * @author jerry_jy
 * @create 2022-09-28 22:00
 */
public class Exer6 {
    /*
    定义员工类Employee,包含姓名、工号和工资,包含计算奖金方法bonus,普通员工和经理都是员工,计算奖金的方法为工资*奖金系数,普通员工的奖金系数为1.5(常量),经理为2(常量),分别实现bonus方法,创建对象测试。
     */
    public static void main(String[] args) {
        Employee1 commonEmp = new Employee1("普通员工", 101, 2000.0);
        Employee1 VIPEmp = new Employee1("经理", 001, 8000.0);
        System.out.println("普通员工的奖金:" + commonEmp.bonus());
        System.out.println("经理的奖金:" + VIPEmp.bonus());
    }
}
class Employee1 {
    String name;
    int id;
    double salary;
    public Employee1() {
    }
    public Employee1(String name, int id, double salary) {
        this.name = name;
        this.id = id;
        this.salary = salary;
    }
    double COMMON_BONUS = 1.5;
    double VIP_BONUS = 2.0;
    public double bonus() {
        if (id <= 100) {
            double bonus = salary * VIP_BONUS;
            return bonus;
        } else {
            double bonus = salary * COMMON_BONUS;
            return bonus;
        }
    }
    @Override
    public String toString() {
        return "Employee1{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", salary=" + salary +
                '}';
    }
}


相关文章
|
6天前
|
Java
Java面向对象实践小结(含面试题)(下)
Java面向对象实践小结(含面试题)(下)
18 1
|
5天前
|
存储 Java 开发工具
【Java探索之旅】用面向对象的思维构建程序世界
【Java探索之旅】用面向对象的思维构建程序世界
9 0
|
5天前
|
Java
java使用面向对象实现图书管理系统
java使用面向对象实现图书管理系统
|
5天前
|
Java
Java语言---面向对象的三大特征之继承
Java语言---面向对象的三大特征之继承
|
5天前
|
机器学习/深度学习 Java Python
Java面向对象知识体系---基础版
Java面向对象知识体系---基础版
|
6天前
|
存储 缓存 开发框架
Java基础面试题小结(下)
Java基础面试题小结(下)
12 0
|
6天前
|
存储 安全 Java
Java基础面试题小结(上)
Java基础面试题小结(上)
13 0
|
6天前
|
安全 Java 编译器
Java面向对象思想以及原理以及内存图解(下)
Java面向对象思想以及原理以及内存图解(下)
16 0
|
6天前
|
Java
Java面向对象思想以及原理以及内存图解(上)
Java面向对象思想以及原理以及内存图解
17 0
|
6天前
|
设计模式 Java
Java面向对象实践小结(含面试题)(上)
Java面向对象实践小结(含面试题)
13 1