视频制作用什么软件,网站建设推广seo,网站后台在哪里,如何给别人做网站文章目录 第07章_面向对象编程#xff08;进阶#xff09;拓展练习01-关键字#xff1a;this1、Circle类2、MyDate类3、Card类 02-继承性和方法重写4、Person、Student、Teacher类5、DepositCard、CreditCard类6、Employee、Programmer、Designer、Architect类7、判断输出结… 文章目录 第07章_面向对象编程进阶拓展练习01-关键字this1、Circle类2、MyDate类3、Card类 02-继承性和方法重写4、Person、Student、Teacher类5、DepositCard、CreditCard类6、Employee、Programmer、Designer、Architect类7、判断输出结果 03-关键字super8、判断运行结果9、判断运行结果10、判断运行结果11、判断运行结果12、判断运行结果13、判断运行结果14、是否可通过编译15、Person、Student、Teacher类 05-多态性16、Graphic、Circle、Rectangle类17、Employee、SalaryEmployee、HourEmployee、Manager类18、Person、Man、Woman等类19、Person、Man、Woman类及数组20、普通员工、程序员、设计师、架构师21、判断运行结果22、判断运行结果23、判断运行结果24、判断运行结果25、判断运行结果26、判断运行结果27、判断运行结果28、判断运行结果29、判断运行结果30、判断运行结果31、判断运行结果32、判断运行结果 06-Object类33、Triangle类 第07章_面向对象编程进阶拓展练习 01-关键字this
1、Circle类 在com.atguigu.exercise1.bean包中定义一个圆形Circle类。 属性私有化 r半径 构造方法 无参构造方法满参构造方法 成员方法 get/set方法showArea方法打印圆形面积showPerimeter方法打印圆形周长 在com.atguigu.test01.test包中定义测试类TestCircle创建Circle对象并测试。
package com.atguigu.exercise1.bean;public class Circle {private double radius;public Circle(double radius) {this.radius radius;}public Circle() {}public double getRadius() {return radius;}public void setRadius(double radius) {this.radius radius;}public void showArea(){System.out.println(半径为 radius 面积 3.14 * radius * radius);}public void showPerimeter(){System.out.println(半径为 radius 周长 2 * 3.14 * radius);}
}
public class Exercise1 {public static void main(String[] args) {Circle c1 new Circle(1.2);c1.showArea();c1.showPerimeter();}
}
2、MyDate类 在com.atguigu.test02.bean包中定义一个日期MyDate类。 属性 year年month月day日 构造方法 包含year、month、day三个参数的构造方法 成员方法 get/set方法void showDate方法打印日期。“日期:xxx年xx月xx日”boolean isLeapYear()方法判断当前日期是否是闰年 在com.atguigu.test02.test包中定义测试类创建MyDate对象并测试。 代码实现效果如图所示 提示 闰年能被4整除且不能被100整除 能被400整除
public class MyDate {private int year;private int month;private int 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 void showDate() {System.out.println(日期: year 年 month 月 day 日);}public boolean isLeapYear() {return year % 4 0 year % 100 ! 0 || year % 400 0;}
}
public class Exercise2 {public static void main(String[] args) {MyDate my new MyDate(2019,5,13);my.showDate();boolean flag my.isLeapYear();System.out.println(my.getYear() (flag?是闰年:不是闰年));}
}
3、Card类 在com.atguigu.test03.bean包中定义一个扑克Card类。 属性 花色String hua点数String dian 构造方法 包含hua、dian两个参数的构造方法 成员方法 showCard方法打印牌面信息。比如红桃K 在com.atguigu.test03.test包中定义测试类创建Card对象调用showCard方法。
public class Card {private String hua;private String dian;public Card(String hua, String dian) {this.hua hua;this.dian dian;}public Card() {}public String getHua() {return hua;}public void setHua(String hua) {this.hua hua;}public String getDian() {return dian;}public void setDian(String dian) {this.dian dian;}public void showCard() {System.out.println(hua dian);}
}
public class Exercise3 {public static void main(String[] args) {Card c new Card(黑桃, A);c.showCard();}
}
02-继承性和方法重写
4、Person、Student、Teacher类
1声明父类Person类
包含属性姓名年龄性别属性私有化包含get/set方法包含getInfo()方法例如姓名张三年龄23性别男
2声明子类Student类继承Person类
新增属性score成绩属性私有化包含get/set方法重写getInfo()方法例如姓名张三年龄23性别男成绩89
3声明子类Teacher类继承Person类
新增属性salary薪资属性私有化包含get/set方法重写getInfo()方法例如姓名张三年龄23性别男薪资10000
4在测试类的main方法中创建三个类的对象并调用相应的方法测试
public class Person {private String name;private int age;private char gender;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 char getGender() {return gender;}public void setGender(char gender) {this.gender gender;}public String getInfo() {return 姓名 name 年龄 age 性别 gender;}
}public class Student extends Person {private int score;public int getScore() {return score;}public void setScore(int score) {this.score score;}Overridepublic String getInfo(){//方式一
// return 姓名 getName() 年龄 getAge() 性别 getGender() 成绩 score;//方法二return super.getInfo() 成绩 score;}}public class Teacher extends Person {private double salary;public double getSalary() {return salary;}public void setSalary(double salary) {this.salary salary;}Overridepublic String getInfo(){return super.getInfo() 薪资 salary;}
}public class Exercise4 {public static void main(String[] args) {Person p new Person();p.setName(张三);p.setAge(23);p.setGender(男);System.out.println(p.getInfo());Student s new Student();s.setName(王浩);s.setAge(20);s.setGender(男);s.setScore(90);System.out.println(s.getInfo());Teacher t new Teacher();t.setName(宋红康);t.setAge(36);t.setGender(男);t.setSalary(50000);System.out.println(t.getInfo());}
}5、DepositCard、CreditCard类
1声明一个银行储蓄卡DepositCard类
包含属性账户余额属性私有化包含get/set方法public void withdraw(double money)取款取款金额不能为负数否则提示取款金额不能为负数取款金额不能超过账户余额否则提示余额不足public void save(double money)存款存款金额不能为负数否则提示存款金额不能为负数public String getInfo() 可以返回账户和余额
2声明一个银行信用卡CreditCard类继承储蓄卡类 增加属性本月可透支总额度本月已透支金额 重写public void withdraw(double money)可透支 取款金额超过账户余额本月还可透支额度提示超过可透支额度取款金额在账户余额范围内不用透支取款金额超过账户余额但在可透支范围内需要透支 重写public void save(double money) 存款金额不能为负数否则提示存款金额不能为负数本次存款金额只够偿还部分已透支金额本次存款金额除了偿还透支金额还有剩余
3在测试类中分别创建两种卡对象测试
public class DepositCard {private String id;private double balance;public String getId() {return id;}public void setId(String id) {this.id id;}public double getBalance() {return balance;}public void setBalance(double balance) {this.balance balance;}public void withdraw(double money){if(money0){System.out.println(取款金额不能为负数);return;}if(moneybalance){System.out.println(余额不足);return;}balance - money;}public void save(double money){if(money0){System.out.println(存款金额不能为负数);return;}balance money;}public String getInfo(){return 账号 id 余额 balance;}
}public class CreditCard extends DepositCard {private double maxOverdraft;//当月最多可以透支额度private double overdraft;//本月已透支额度public double getMaxOverdraft() {return maxOverdraft;}public void setMaxOverdraft(double maxOverdraft) {this.maxOverdraft maxOverdraft;}public double getOverdraft() {return overdraft;}public void setOverdraft(double overdraft) {this.overdraft overdraft;}Overridepublic void withdraw(double money) {//超过可透支额度if(money getBalance() maxOverdraft - overdraft){System.out.println(超过可透支额度);return;}//不用透支if(money getBalance()){super.withdraw(money);return;}//需要透支overdraft money - getBalance();setBalance(0);}Overridepublic void save(double money) {if(money 0){System.out.println(存款金额不能为负数);return;}//偿还部分透支金额if(money overdraft){overdraft - money;return;}//偿还所有透支金额还有剩余setBalance(getBalance() (money - overdraft));overdraft 0;}Overridepublic String getInfo() {return super.getInfo() 本月可透支总额度 maxOverdraft 本月已透支 overdraft 本月还可透支 (maxOverdraft - overdraft);}
}
//测试1
public class Exercise5 {public static void main(String[] args) {DepositCard d new DepositCard();d.setId(11111);d.setBalance(500);System.out.println(初始情况 d.getInfo());d.withdraw(200);System.out.println(取款200后 d.getInfo());d.save(100);System.out.println(存款100后 d.getInfo());d.save(-100);System.out.println(存款-100后 d.getInfo());d.withdraw(-100);System.out.println(取款-100后 d.getInfo());d.withdraw(500);System.out.println(取款500后 d.getInfo());}
}//测试2
public class Exercise5 {public static void main(String[] args) {CreditCard d new CreditCard();d.setId(11111);d.setBalance(500);d.setMaxOverdraft(1000);System.out.println(初始情况 d.getInfo());d.withdraw(200);System.out.println(取款200后 d.getInfo());d.withdraw(800);System.out.println(取款800后 d.getInfo());d.withdraw(500);System.out.println(取款500后 d.getInfo());d.save(100);System.out.println(存款100后 d.getInfo());d.save(-100);System.out.println(存款-100后 d.getInfo());d.withdraw(-100);System.out.println(取款-100后 d.getInfo());d.withdraw(500);System.out.println(取款500后 d.getInfo());d.save(2000);System.out.println(存款2000后 d.getInfo());}
}6、Employee、Programmer、Designer、Architect类
1、在包中声明员工类、程序员类、设计师类、架构师类 员工类属性编号、姓名、年龄、手机号码程序员类属性编程语言设计师类属性奖金架构师类属性持有股票数量
要求属性私有化无参有参构造get/setgetInfo方法考虑重写
2、在包中声明测试类Exercise6
创建各种类的对象并测试
public class Employee {private int id;private String name;private int age;private String tel;public Employee() {super();}public Employee(int id, String name, int age, String tel) {super();this.id id;this.name name;this.age age;this.tel tel;}public int getId() {return id;}public void setId(int id) {this.id id;}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 getTel() {return tel;}public void setTel(String tel) {this.tel tel;}public String getInfo(){return 编号 name 姓名 name 年龄 age 电话 tel;}
}
public class Programmer extends Employee{private String language;public Programmer() {super();}public Programmer(int id, String name, int age, String tel, String language) {super(id, name, age, tel);this.language language;}public String getLanguage() {return language;}public void setLanguage(String language) {this.language language;}Overridepublic String getInfo() {return super.getInfo() 编程语言 language;}
}public class Designer extends Programmer {private double bonus;public Designer() {super();}public Designer(int id, String name, int age, String tel, String language, double bonus) {super(id, name, age, tel, language);this.bonus bonus;}public double getBonus() {return bonus;}public void setBonus(double bonus) {this.bonus bonus;}Overridepublic String getInfo() {return super.getInfo() 奖金 bonus;}}
public class Architect extends Designer {private int stock;public Architect() {super();}public Architect(int id, String name, int age, String tel, String language, double bonus, int stock) {super(id, name, age, tel, language, bonus);this.stock stock;}public int getStock() {return stock;}public void setStock(int stock) {this.stock stock;}Overridepublic String getInfo() {return super.getInfo() 持有股票数 stock;}
}public class Exercise6 {public static void main(String[] args) {Employee emp new Employee(1, 张三, 23, 10086);Programmer pro new Programmer(2, 李四, 24, 10010, java);Designer de new Designer(3, 王五, 25, 114, python, 2000);Architect a new Architect(4, 赵六, 26, 110, java, 3000, 100);System.out.println(emp.getInfo());System.out.println(pro.getInfo());System.out.println(de.getInfo());System.out.println(a.getInfo());}
}
7、判断输出结果 public class Exercise7 {public static void main(String[] args) {Father f new Father();Son s new Son();System.out.println(f.getInfo());System.out.println(s.getInfo());s.setInfo(尚硅谷);System.out.println(f.getInfo());System.out.println(s.getInfo());}
}
class Father{private String info atguigu;public void setInfo(String info){this.info info;}public String getInfo(){return info;}
}
class Son extends Father{}/*
1、私有的属性是否可以被继承到子类中
1如果从可以访问性的角度来说不能因为在子类中不能直接访问父类的私有的属性但是可以通过get/set操作
2如果从类的概念来说
类是一类具有相同特性属性、方法等的事物的抽象描述
那么子类是从父类派生出来的那么子类是有父类的这个特征的即有这个属性的2、每一个对象的非静态属性是独立的其中一个对象修改和另一个对象是无关的*/03-关键字super
8、判断运行结果
public class Exercise8 {public static void main(String[] args) {new Child(mike);}
}class People {private String name;public People() {System.out.print(1);}public People(String name) {System.out.print(2);this.name name;}
}class Child extends People {People father;public Child(String name) {System.out.print(3);father new People(name F);}public Child() {System.out.print(4);}
}
9、判断运行结果
public class Exercise9 {public static void main(String[] args) {Father f new Father();Child c new Child();}
}
class Father {public Father(){System.out.println(father()...);}
}
class Child extends Father{public Child(){System.out.println(child()...);}
}10、判断运行结果
public class Exercise10 {public static void main(String[] args) {new A(new B());}
}class A {public A() {System.out.println(A);}public A(B b) {this();System.out.println(AB);}
}class B {public B() {System.out.println(B);}
}
11、判断运行结果
public class Son extends Father{private String name son;public static void main(String[] args) {Son son new Son();System.out.println(son.getName());}
}
class Father {private String name father;public String getName() {return name;}
}/** 当父类与子类有同名的属性时* 通过子类对象调用getName()访问的是父类的name还是子类的name* 那么要看子类是否重写如果没有重写就是父类的重写了就是子类的。*/12、判断运行结果
public class Exercise12 {public static void main(String[] args) {Father f new Father();Son s new Son();System.out.println(f.getInfo());System.out.println(s.getInfo());s.test();System.out.println(-----------------);s.setInfo(大硅谷);System.out.println(f.getInfo());System.out.println(s.getInfo());s.test();}
}
class Father{private String info atguigu;public void setInfo(String info){this.info info;}public String getInfo(){return info;}
}
class Son extends Father{private String info 尚硅谷;public void test(){System.out.println(this.getInfo());System.out.println(super.getInfo());}
}/*
1、私有的属性是否可以被继承到子类中
1如果从可以访问性的角度来说不能因为在子类中不能直接访问父类的私有的属性但是可以通过get/set操作
2如果从类的概念来说
类是一类具有相同特性属性、方法等的事物的抽象描述
那么子类是从父类派生出来的那么子类是有父类的这个特征的即有这个属性的2、每一个对象的非静态属性是独立的其中一个对象修改和另一个对象是无关的3、当子类有与父类的属性同名时那么通过子类对象调用get/set方法操作的是父类继承还是子类自己的属性呢
要看子类是否重写
如果没有重写操作的都是父类的不管是直接getInfo()还是this.getInfo()还是super.getInfo()
如果重写了如果通过子类对象调用操作的是子类的例如getInfo()还是this.getInfo()如果通过super.调用的操作的是父类的。*/ 13、判断运行结果
public class Exercise13 {public static void main(String[] args) {Father f new Father();Son s new Son();System.out.println(f.getInfo());System.out.println(s.getInfo());s.test();System.out.println(-----------------);s.setInfo(大硅谷);System.out.println(f.getInfo());System.out.println(s.getInfo());s.test();}
}
class Father{private String info atguigu;public void setInfo(String info){this.info info;}public String getInfo(){return info;}
}
class Son extends Father{private String info 尚硅谷;public void setInfo(String info){this.info info;}public String getInfo(){return info;}public void test(){System.out.println(this.getInfo());System.out.println(super.getInfo());}
}/*
1、私有的属性是否可以被继承到子类中
1如果从可以访问性的角度来说不能因为在子类中不能直接访问父类的私有的属性但是可以通过get/set操作
2如果从类的概念来说
类是一类具有相同特性属性、方法等的事物的抽象描述
那么子类是从父类派生出来的那么子类是有父类的这个特征的即有这个属性的2、每一个对象的非静态属性是独立的其中一个对象修改和另一个对象是无关的3、当子类有与父类的属性同名时那么通过子类对象调用get/set方法操作的是父类继承还是子类自己的属性呢
要看子类是否重写
如果没有重写操作的都是父类的不管是直接getInfo()还是this.getInfo()还是super.getInfo()
如果重写了如果通过子类对象调用操作的是子类的例如getInfo()还是this.getInfo()如果通过super.调用的操作的是父类的。*/14、是否可通过编译
如下代码是否可以编译通过如果能结果是什么如果不能为什么
public class Person{public Person(){System.out.println(this is a Person.);}
}
public class Teacher extends Person{private String name tom;public Teacher(){System.out.println(this is a teacher.);super();}public static void main(String[] args){Teacher tea new Teacher();System.out.println(this.name);}
}剖析
public class Teacher extends Person{private String name tom;public Teacher(){System.out.println(this is a teacher.);
// super();//错误super()必须在构造器首行}public static void main(String[] args){Teacher tea new Teacher();
// System.out.println(this.name);//错误static方法中不能使用this}
}
15、Person、Student、Teacher类
1声明父类Person类
包含属性姓名年龄性别属性私有化包含get/set方法提供无参构造public Person()提供有参构造public Person(String name, int age, char gender)包含getInfo()方法例如姓名张三年龄23性别男
2声明子类Student类继承Person类
新增属性score成绩属性私有化包含get/set方法提供无参构造public Student()提供有参构造public Student(String name, int age, char gender, int score)重写getInfo()方法例如姓名张三年龄23性别男成绩89
3声明子类Teacher类继承Person类
新增属性salary薪资属性私有化包含get/set方法提供无参构造public Teacher()提供有参构造public Teacher(String name, int age, char gender, double salary)重写getInfo()方法例如姓名张三年龄23性别男薪资10000
4在测试类的main方法中用有参构造创建三个类的对象并调用相应的方法测试
public class Person {private String name;private int age;private char gender;public Person() {}public Person(String name, int age, char gender) {this.name name;this.age age;this.gender gender;}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 char getGender() {return gender;}public void setGender(char gender) {this.gender gender;}public String getInfo() {return 姓名 name 年龄 age 性别 gender;}
}
public class Student extends Person {private int score;public Student() {}public Student(String name, int age, char gender, int score) {super(name, age, gender);this.score score;}public int getScore() {return score;}public void setScore(int score) {this.score score;}Overridepublic String getInfo(){return super.getInfo() 成绩 score;}
}public class Teacher extends Person {private double salary;public Teacher() {}public Teacher(String name, int age, char gender, double salary) {super(name, age, gender);this.salary salary;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary salary;}Overridepublic String getInfo(){return super.getInfo() 薪资 salary;}
}public class Exercise15 {public static void main(String[] args) {Person p new Person(张三,23,男);System.out.println(p.getInfo());Student s new Student(王浩,20,男,90);System.out.println(s.getInfo());Teacher t new Teacher(宋红康,37,男,50000);System.out.println(t.getInfo());}
}05-多态性
16、Graphic、Circle、Rectangle类
1父类Graphic图形
public double area()方法返回0.0public double perimeter()方法返回0.0public String getInfo()方法返回图形面积和图形周长
2子类Circle圆类继承Graphic图形
包含属性radius属性私有化包含get/set方法重写area()求面积方法重写perimeter()求周长方法重写getInfo()方法返回圆的半径面积和周长
3子类矩形Rectangle继承Graphic图形
包含属性length、width属性私有化包含get/set方法重写area()求面积方法重写perimeter()求周长方法重写getInfo()方法返回长和宽面积、周长信息
4在测试类的main方法中创建多个圆和矩形对象放到Graphic[]类型的数组中并按照面积从小到大排序输出。
public class Graphic {public double area(){return 0.0;}public double perimeter(){return 0.0;}public String getInfo() {return 面积 area() 周长 perimeter();}
}public class Rectangle extends Graphic{private double length;private double width;public double getLength() {return length;}public void setLength(double length) {this.length length;}public double getWidth() {return width;}public void setWidth(double width) {this.width width;}Overridepublic double area(){return length * width;}Overridepublic double perimeter(){return 2 * (length width);}Overridepublic String getInfo() {return 矩形长 length 宽 width super.getInfo();}
}
public class Circle extends Graphic{private double radius;public double getRadius() {return radius;}public void setRadius(double radius) {this.radius radius;}Overridepublic double area(){return Math.PI * radius * radius;}Overridepublic double perimeter(){return 2 * Math.PI * radius;}Overridepublic String getInfo() {return 圆半径 radius , super.getInfo();}
}
public class Exercise16 {public static void main(String[] args) {//用一个数组用来存储各种图形的对象并且按照各种图形的面积进行排序Circle c1 new Circle();c1.setRadius(2.5);Circle c2 new Circle();c2.setRadius(1.5);Rectangle r1 new Rectangle();r1.setLength(5);r1.setWidth(1);Rectangle r2 new Rectangle();r2.setLength(4);r2.setWidth(2);Graphic[] shapes new Graphic[4];shapes[0] c1;shapes[1] c2;shapes[2] r1;shapes[3] r2;System.out.println(排序前);for (int i 0; i shapes.length; i) {System.out.println(shapes[i].getInfo());}//按照面积排序for (int i 1; i shapes.length; i) {for (int j 0; j shapes.length - i; j) {if(shapes[j].area() shapes[j1].area()){Graphic temp shapes[j];shapes[j] shapes[j1];shapes[j1] temp;}}}System.out.println(排序后);for (int i 0; i shapes.length; i) {System.out.println(shapes[i].getInfo());}}
}17、Employee、SalaryEmployee、HourEmployee、Manager类
1声明一个父类Employee员工类型
有姓名属性私有化提供get/set方法提供有参构造public Employee(String name)public double earning()代表实发工资返回0.0public String getInfo()显示姓名和实发工资
2声明MyDate类型
有int类型的年月日属性私有化提供get/set方法提供有参构造public MyDate(int year, int month, int day)提供public String getInfo()返回“xxxx年xx月xx日”
3声明一个子类SalaryEmployee正式工继承父类Employee
增加属性double类型的薪资MyDate类型的出生日期私有化提供get/set方法提供有参构造public SalaryEmployee(String name, double salary, MyDate birthday)提供有参构造public SalaryEmployee(String name,double salary, int year, int month ,int day)重写方法public double earning()返回实发工资 实发工资 薪资重写方法public String getInfo()显示姓名和实发工资、生日
4声明一个子类HourEmployee小时工继承父类Employee
有属性double类型的工作小时数和每小时多少钱提供有参构造public HourEmployee(String name, double moneyPerHour)提供有参构造public HourEmployee(String name, double moneyPerHour, double hour)重写方法public double earning()返回实发工资 实发工资 每小时多少钱 * 小时数重写方法public String getInfo()显示姓名和实发工资时薪工作小时数
5声明一个子类Manager经理继承SalaryEmployee
增加属性奖金比例私有化提供get/set方法提供有参构造public Manager(String name, double salary, MyDate birthday, double bonusRate)提供有参构造public Manager(String name,double salary, int year, int month ,int day, double bonusRate)重写方法public double earning()返回实发工资 实发工资 薪资 *(1奖金比例)重写方法public String getInfo()显示姓名和实发工资生日奖金比例
6声明一个员工数组存储各种员工你现在是人事遍历查看每个人的详细信息并统计实发工资总额通知财务准备资金。
7从键盘输入当期月份值如果他是正式工包括SalaryEmployee和Manager并且是本月生日的通知领取生日礼物。
public class Employee {private String name;public Employee(String name) {this.name name;}public String getName() {return name;}public void setName(String name) {this.name name;}public double earning(){return 0.0;}public String getInfo(){return 姓名 name 实发工资 earning();}
}
public class MyDate {private int year;private int month;private int day;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 String getInfo(){return year 年 month 月 day 日;}
}
public class SalaryEmployee extends Employee {private double salary;private MyDate birthday;public SalaryEmployee(String name, double salary, MyDate birthday) {super(name);this.salary salary;this.birthday birthday;}public SalaryEmployee(String name, double salary,int year, int month, int day) {super(name);this.salary salary;this.birthday new MyDate(year, month ,day);}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary salary;}public MyDate getBirthday() {return birthday;}public void setBirthday(MyDate birthday) {this.birthday birthday;}Overridepublic double earning() {return salary;}Overridepublic String getInfo() {return super.getInfo() 生日 birthday.getInfo();}
}
public class HourEmployee extends Employee {private double hour;private double moneyPerHour;public HourEmployee(String name, double hour) {super(name);this.hour hour;}public HourEmployee(String name, double hour, double moneyPerHour) {super(name);this.hour hour;this.moneyPerHour moneyPerHour;}public double getHour() {return hour;}public void setHour(double hour) {this.hour hour;}public double getMoneyPerHour() {return moneyPerHour;}public void setMoneyPerHour(double moneyPerHour) {this.moneyPerHour moneyPerHour;}Overridepublic double earning() {return hour * moneyPerHour;}Overridepublic String getInfo() {return super.getInfo() 时薪 moneyPerHour ,工作小时数 hour;}
}
public class Manager extends SalaryEmployee {private double bonusRate;public Manager(String name, double salary, MyDate birthday, double bonusRate) {super(name, salary, birthday);this.bonusRate bonusRate;}public Manager(String name, double salary, int year, int month, int day, double bonusRate) {super(name, salary, year, month, day);this.bonusRate bonusRate;}public double getBonusRate() {return bonusRate;}public void setBonusRate(double bonusRate) {this.bonusRate bonusRate;}Overridepublic double earning() {return super.earning() * (1 bonusRate);}Overridepublic String getInfo() {return super.getInfo() ,奖金比例 bonusRate;}
}
public class Exercise17 {public static void main(String[] args) {Employee[] all new Employee[3];all[0] new SalaryEmployee(张三,15000,new MyDate(1995,5,1));all[1] new Manager(老王,20000,1990,6,1,0.1);all[2] new HourEmployee(李四,50,100);double sum 0;for (int i 0; i all.length; i) {System.out.println(all[i].getInfo());sum all[i].earning();}System.out.println(实发工资总额 sum);Scanner input new Scanner(System.in);System.out.print(请输入月份);int month input.nextInt();for (int i 0; i all.length; i) {if(all[i] instanceof SalaryEmployee){SalaryEmployee s (SalaryEmployee) all[i];if(s.getBirthday().getMonth() month){System.out.println(s.getName() 生日快乐请到人事部门领取生日大礼包);}}}input.close();}
}18、Person、Man、Woman等类
案例
1、在包中声明人Person、男人Man、女人Woman类
1在Person类中包含
①public void eat()打印吃饭
②public void toilet()打印上洗手间
2在Man类中包含
①重写上面的方法
②增加 public void smoke()打印抽烟
3在Woman类中包含
①重写上面的方法
②增加 public void makeup()打印化妆
2、在包中声明测试类Test09
1public static void meeting(Person… ps)
在该方法中每一个人先吃饭然后上洗手间然后如果是男人随后抽根烟如果是女人随后化个妆
2public static void main(String[] args)
在主方法中创建多个男人和女人对象并调用meeting()方法进行测试
public class Person {public void eat(){System.out.println(吃饭);}public void toilet(){System.out.println(上洗手间);}
}
public class Man extends Person{Overridepublic void eat() {System.out.println(细嚼慢咽吃饭);}Overridepublic void toilet() {System.out.println(站着上洗手间);}public void smoke(){System.out.println(抽烟爽爽);}
}
public class Woman extends Person{Overridepublic void eat() {System.out.println(狼吞虎咽吃饭);}Overridepublic void toilet() {System.out.println(坐着上洗手间);}public void makeup(){System.out.println(化妆美美);}}
public class Exercise18 {public static void main(String[] args) {meeting(new Woman(),new Man(), new Woman(), new Man());}public static void meeting(Person... ps){for (int i 0; i ps.length; i) {ps[i].eat();ps[i].toilet();if(ps[i] instanceof Woman){((Woman)ps[i]).makeup();}else if(ps[i] instanceof Man){((Man)ps[i]).smoke();}}}
}19、Person、Man、Woman类及数组
1在Person类中包含
属性姓名年龄职业public void eat()打印xx吃饭xx代表姓名public void toilet()打印xx上洗手间public String getInfo()返回姓名、年龄、职业信息。
2在Man类中包含
重写eat()xx狼吞虎咽吃饭增加 public void smoke()打印xx抽烟
3在Woman类中包含
重写eat()xx细嚼慢咽吃饭增加 public void makeup()打印xx化妆
4周末一群男男女女相亲在测试类创建不同对象放在Person[]数组中
遍历数组自我介绍再次遍历数组调用吃饭方法吃完饭最后遍历数组都去上厕所男的上完厕所抽烟女的上完厕所补妆。
public class Person {private String name;private int age;private String job;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 getJob() {return job;}public void setJob(String job) {this.job job;}public void eat() {System.out.println(name 吃饭);}public void toilet() {System.out.println(name 上洗手间);}public String getInfo() {return 姓名 name 年龄 age 职业 job;}
}
public class Man extends Person {Overridepublic void eat() {System.out.println(getName() 狼吞虎咽的吃饭);}public void smoke(){System.out.println(getName() 抽烟);}
}
public class Woman extends Person{Overridepublic void eat() {System.out.println(getName() 细嚼慢咽的吃饭);}public void makeup(){System.out.println(getName() 化妆);}
}
public class Exercise19 {public static void main(String[] args) {Person[] arr new Person[4];arr[0] new Man();arr[0].setName(张三);arr[0].setAge(23);arr[0].setJob(Java中级工程师);arr[1] new Man();arr[1].setName(李四);arr[1].setAge(24);arr[1].setJob(大数据工程师);arr[2] new Woman();arr[2].setName(翠花);arr[2].setAge(22);arr[2].setJob(UI设计师);arr[3] new Woman();arr[3].setName(如花);arr[3].setAge(23);arr[3].setJob(前端设计师);System.out.println(------------初次见面--------------);for (int i 0; i arr.length; i) {System.out.println(arr[i].getInfo());}System.out.println(-------------开始聚餐--------------);for (int i 0; i arr.length; i) {arr[i].eat();}System.out.println(---------------饭后休息-------------);for (int i 0; i arr.length; i) {arr[i].toilet();if(arr[i] instanceof Man){Man man (Man) arr[i];man.smoke();}else if(arr[i] instanceof Woman){Woman woman (Woman) arr[i];woman.makeup();}}}
}
20、普通员工、程序员、设计师、架构师
1普通员工Employee类
包含编号、姓名、年龄、工资属性私有化提供无参构造提供有参构造Employee(int id, String name, int age, double salary)提供get/set方法提供String say()方法返回员工基本信息提供String getInfo()方法返回员工基本信息
2程序员Programmer类继承普通员工类
提供无参构造提供有参构造Programmer(int id, String name, int age, double salary)重写String getInfo()方法增加职位“程序员”信息
3设计师Designer类继承程序员类
增加奖金属性提供无参构造提供有参构造Designer(int id, String name, int age, double salary, double bonus)重写String getInfo()方法增加职位“设计师”和奖金信息
4架构师Architect类继承设计师类
增加股票属性提供无参构造提供有参构造Architect(int id, String name, int age, double salary, double bonus, int stock)重写String getInfo()方法增加职位“架构师”和奖金、股票信息
5在测试类中创建员工数组并存储1个普通员工对象2个程序员对象1个架构师对象1个设计师对象 public class Employee {private int id;private String name;// : 姓名private int age;// : 年龄private double salary;// : 工资public Employee() {}public Employee(int id, String name, int age, double salary) {this.id id;this.name name;this.age age;this.salary salary;}public int getId() {return id;}public void setId(int id) {this.id id;}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 double getSalary() {return salary;}public void setSalary(double salary) {this.salary salary;}public String say(){return id \t\t name \t age \t\t salary;}public String getInfo() {return say();}
}
public class Programmer extends Employee {public Programmer() {}public Programmer(int id, String name, int age, double salary) {super(id, name, age, salary);}Overridepublic String getInfo() {return super.say() \t\t程序员;}
}
public class Designer extends Programmer {private double bonus;// : 奖金public Designer() {}public Designer(int id, String name, int age, double salary, double bonus) {super(id, name, age, salary);this.bonus bonus;}public double getBonus() {return bonus;}public void setBonus(double bonus) {this.bonus bonus;}Overridepublic String getInfo() {return super.say() \t\t设计师 \t bonus;}
}
public class Architect extends Designer {private int stock;// : 股票public Architect() {}public Architect(int id, String name, int age, double salary, double bonus, int stock) {super(id, name, age, salary, bonus);this.stock stock;}public int getStock() {return stock;}public void setStock(int stock) {this.stock stock;}Overridepublic String getInfo() {return super.say() \t\t架构师 \t getBonus() \t stock;}
}public class Exercise20 {public static void main(String[] args) {Employee[] arr new Employee[5];arr[0] new Employee(1,段誉,22,3000);arr[1] new Architect(2,令狐冲,32,18000,15000,2000);arr[2] new Programmer(3,任我行,23,7000);arr[3] new Programmer(4,张三丰,24,7300);arr[4] new Designer(5,周芷若,28,10000,5000);System.out.println(----------------员工信息管理-------------------);System.out.println(编号\t姓名\t年龄\t工资\t\t职位\t奖金\t股票);for (int i 0; i arr.length; i) {arr[i].setId(i1);System.out.println(arr[i].getInfo());}System.out.println(----------------------------------------------);}
}21、判断运行结果
如下代码是否可以编译通过如果能结果是什么如果不能为什么
public class Father{private String name atguigu;int age 0;
}
public class Child extends Father{public String grade;public static void main(String[] args){Father f new Child();System.out.println(f.name);}
}public class Child extends Father{public String grade;public static void main(String[] args){Father f new Child();
// System.out.println(f.name);//编译错误因为name私有化}
}
22、判断运行结果
public class Exercise22 {public static void main(String[] args) {Father f new Father();Father s new Son();Father d new Daughter();MyClass my new MyClass();/*第一个问题my有没有多态引用没有从my角度来说不需要考虑多态现象。第二个问题my调用的method方法去哪个类找只MyClass类中找就可以和其他类无关。第三个问题method方法有三个形式即重载的形式那么怎么确定调用的是哪一个重载方法找寻原则A先找最匹配的什么叫最匹配的实参的“编译时”类型和形参的“声明”类型一致个数也一致。B再找唯一可以兼容的什么叫兼容实参的“编译时”类型 形参的“声明”类型形参是可变参数的话实参的个数在它允许的范围内*/my.method(f);//father/*实参f的编译时类型是 Father和哪个方法的形参最匹配呢public void method(Father f) 就它了*/my.method(s);//father/*实参s的编译时类型是 Father和哪个方法的形参最匹配呢public void method(Father f) 就它了*/my.method(d);//father/*实参d的编译时类型是 Father和哪个方法的形参最匹配呢public void method(Father f) 就它了*/}
}
class MyClass{public void method(Father f) {System.out.println(father);}public void method(Son s) {System.out.println(son);}public void method(Daughter f) {System.out.println(daughter);}
}
class Father{}
class Son extends Father{}
class Daughter extends Father{}23、判断运行结果
public class Exercise23 {public static void main(String[] args) {Father f new Father();Son s new Son();Daughter d new Daughter();MyClass my new MyClass();//my没有多态引用只看MyClass类就可以了my.method(f);//father/*实参f的编译时类型仍然是Father找最匹配的public void method(Father f)*/my.method(s);//son/*实参s的编译时类型是Son找最匹配的public void method(Son s)*/my.method(d);//father/*实参d的编译时类型是Daughter找最匹配没有找兼容的public void method(Father f)*/}
}
class MyClass{public void method(Father f) {System.out.println(father);}public void method(Son s) {System.out.println(son);}
}
class Father{}
class Son extends Father{}
class Daughter extends Father{}
24、判断运行结果
public class Exercise24 {public static void main(String[] args) {Father f new Father();Son s new Son();Daughter d new Daughter();MyClass my new MySub();//my有多态引用了而且method是虚方法/*如何确定它执行的是哪个方法呢1编译时去my的编译时类型MyClass中找合适的方法2运行时去my的运行时类型MySub中看是否有对刚刚找到的方法进行了“重写”*/my.method(f);//father/*1编译时去my的编译时类型MyClass中找合适的方法实参f的编译时类型是Father找最匹配的public void method(Father f)2运行时去my的运行时类型MySub中看是否有对刚刚找到的方法进行了“重写”没有重写仍然执行刚刚在MyClass中找到的合适的方法*/my.method(s);//son/*1编译时去my的编译时类型MyClass中找合适的方法实参s的编译时类型是Son找最匹配的public void method(Son s)2运行时去my的运行时类型MySub中看是否有对刚刚找到的方法进行了“重写”没有重写仍然执行刚刚在MyClass中找到的合适的方法*/my.method(d);//father/*1编译时去my的编译时类型MyClass中找合适的方法实参s的编译时类型是Daughter找最匹配的没有找兼容的public void method(Father f)2运行时去my的运行时类型MySub中看是否有对刚刚找到的方法进行了“重写”没有重写仍然执行刚刚在MyClass中找到的合适的方法*/}
}
class MyClass{public void method(Father f) {System.out.println(father);}public void method(Son s) {System.out.println(son);}
}
class MySub extends MyClass{public void method(Daughter d) {//这个不是重写因为形参列表不同System.out.println(daughter);}
}
class Father{}
class Son extends Father{}
class Daughter extends Father{}
25、判断运行结果
public class Exercise25 {public static void main(String[] args) {Father f new Father();Son s new Son();Daughter d new Daughter();MyClass my new MySub();//my有多态引用了而且method是虚方法/*如何确定它执行的是哪个方法呢1编译时去my的编译时类型MyClass中找合适的方法2运行时去my的运行时类型MySub中看是否有对刚刚找到的方法进行了“重写”*/my.method(f);//daughter/*1编译时去my的编译时类型MyClass中找合适的方法实参f的编译时类型是Father找最匹配的public void method(Father f)2运行时去my的运行时类型MySub中看是否有对刚刚找到的方法进行了“重写”有重写一定是执行重写后的代码*/my.method(s);//son/*1编译时去my的编译时类型MyClass中找合适的方法实参s的编译时类型是Son找最匹配的public void method(Son s)2运行时去my的运行时类型MySub中看是否有对刚刚找到的方法进行了“重写”没有重写仍然执行刚刚在MyClass中找到的合适的方法*/my.method(d);//daughter/*1编译时去my的编译时类型MyClass中找合适的方法实参s的编译时类型是Daughter找最匹配的没有找兼容的public void method(Father f)2运行时去my的运行时类型MySub中看是否有对刚刚找到的方法进行了“重写”有重写执行的是重写的方法体 daughter*/}
}
class MyClass{public void method(Father f) {System.out.println(father);}public void method(Son s) {System.out.println(son);}
}
class MySub extends MyClass{//有变化public void method(Father d) {//是重写System.out.println(daughter);}
}
class Father{}
class Son extends Father{}
class Daughter extends Father{}26、判断运行结果
考核点属性与多态无关
public class Exercise26 {public static void main(String[] args) {A a new B();System.out.println(a.num);System.out.println(((B)a).num);System.out.println(((A)((B)a)).num);System.out.println(-------------------);B b new B();System.out.println(b.num);System.out.println(((A)b).num);System.out.println(((B)((A)b)).num);}
}
class A{int num 1;
}
class B extends A{int num 2;
}/** 多态性现象编译时类型与运行时类型不一致* 但是多态性是针对方法来说方法有动态绑定一说。* 属性没有多态性。属性都是按照编译时类型处理的。*/
public class Exercise26 {public static void main(String[] args) {A a new B();System.out.println(a.num);//a编译时类型就是A 1System.out.println(((B)a).num);//编译后因为a被强制成B类是B类型 2System.out.println(((A)((B)a)).num);//编译后a转成B又转成A是A类型 1System.out.println(-------------------);B b new B();System.out.println(b.num);//b编译时类型就是B 2System.out.println(((A)b).num);//b被强制升级为A类型按A类型处理 1System.out.println(((B)((A)b)).num);//b先转A又转B最终是B类型 2}
}
class A{int num 1;
}
class B extends A{int num 2;
}27、判断运行结果
考核点实例初始化方法属性与多态无关
public class Exercise27 {public static void main(String[] args) {Father f new Son();System.out.println(f.x);}
}
class Father{int x 10;public Father(){this.print();x 20;}public void print(){System.out.println(Father.x x);}
}
class Son extends Father{int x 30;public Son(){this.print();x 40;}public void print(){System.out.println(Son.x x);}
}/** 1、Father f new Son();* 实例初始化的过程* 1父类的实例初始化* init(){* x 10;//父类的x* this.print();//子类的print因为this代表的是正在创建的子类对象而子类重写了print所以是子类的print* System.out.println(Son.x x);//子类的x此时还没有赋值那么是默认值x0x 20;//父类的x* }* 2子类的实例初始化* init(){* x 30;//子类的x* this.print();//子类的print* System.out.println(Son.x x);//子类的x此时已经赋值x30x 40;//子类的x* }* * 2、执行System.out.println(f.x);* 属性没有多态性只看编译时类型那么此时f.x表示父类的x*/
public class Exercise27 {public static void main(String[] args) {Father f new Son();System.out.println(f.x);}
}
class Father{int x 10;public Father(){this.print();x 20;}public void print(){System.out.println(Father.x x);}
}
class Son extends Father{int x 30;public Son(){this.print();x 40;}public void print(){System.out.println(Son.x x);}
}28、判断运行结果
考核点多态重写实例初始化过程
public class Exercise28 {public static void main(String[] args) {Base b1 new Base();Base b2 new Sub();}
}class Base {Base() {method(100);}public void method(int i) {System.out.println(base : i);}
}class Sub extends Base {Sub() {super.method(70);}public void method(int j) {System.out.println(sub : j);}
}/** 1、Base b1 new Base();* 父类的实例初始化和子类无关* * init(){* method(100);* System.out.println(base : i); base:100* }* * 2、Base b2 new Sub();* 1 父类的实例初始化* * init(){* method(100);//执行了子类重写的method()* System.out.println(sub : j); sub:100* }* * 2子类的实例初始化* init(){* super.method(70);* System.out.println(base : i); base:70* }*/
public class Exercise28 {public static void main(String[] args) {Base b1 new Base();Base b2 new Sub();}
}class Base {Base() {method(100);}public void method(int i) {System.out.println(base : i);}
}class Sub extends Base {Sub() {super.method(70);}public void method(int j) {System.out.println(sub : j);}
}29、判断运行结果
考核知识点多态、重载、重写
public class Exercise29 {public static void main(String[] args) {A a1 new A();A a2 new B();B b new B();C c new C();D d new D();System.out.println((1) a1.show(b));System.out.println((2) a2.show(d));System.out.println((3) b.show(c));System.out.println((4) b.show(d));}
}
class A{public String show(D obj){return (A and D);}public String show(A obj){return A and A;}
}
class B extends A{public String show(B obj){return B and B;}public String show(A obj){return B and A;}
}
class C extends B{}
class D extends B{}/** 1、分析方法列表和继承关系* A类* public String show(D obj) * public String show(A obj)* B类* public String show(D obj)继承的* public String show(A obj)重写* public String show(B obj)自定义的* C-B-A* D-B-A* * 2、方法重载找最合适的形参类型* 3、方法重写如果子类重写就执行重写的* 4、分析执行结果* a1.show(b)a1没有多态引用直接找A类的方法b是B类对象只能选择public String show(A obj) A and A* a2.show(d)a2多态引用执行子类的方法d是D类对象选最合适的public String show(D obj) A and D* b.show(c)b没有多态引用直接找B类的方法c是C类的对象选择最合适的public String show(B obj) B and B* b.show(d)b没有多态引用直接找B类的方法d是D类对象选最合适的public String show(D obj) A and D*/
public class Exercise29 {public static void main(String[] args) {A a1 new A();A a2 new B();B b new B();C c new C();D d new D();System.out.println((1) a1.show(b));System.out.println((2) a2.show(d));System.out.println((3) b.show(c));System.out.println((4) b.show(d));}
}class A {public String show(D obj) {return (A and D);}public String show(A obj) {return A and A;}
}class B extends A {public String show(B obj) {return B and B;}public String show(A obj) {return B and A;}
}class C extends B {}class D extends B {}30、判断运行结果
考核知识点多态、重载、重写
public class Exercise30 {public static void main(String[] args) {A a1 new A();A a2 new B();B b new B();C c new C();D d new D();System.out.println((1) a1.show(b));System.out.println((2) a2.show(d));System.out.println((3) b.show(c));System.out.println((4) b.show(d));}
}class A {public String show(C obj) {return (A and C);}public String show(A obj) {return A and A;}
}class B extends A {public String show(B obj) {return B and B;}public String show(A obj) {return B and A;}
}class C extends B {}class D extends B {}/** 1、分析每个类的方法列表和继承关系* A类* public String show(C obj) * public String show(A obj)* B类* public String show(C obj)继承的* public String show(A obj)重写* public String show(B obj)自定义的* C-B-A* D-B-A* * 2、方法重载找最合适的形参类型* 3、方法重写如果子类重写就执行重写的* 4、如果特殊的重载那么多态时编译时先从父类中查找最合适的形参类型然后如果子类如果有重写执行子类重写的如果没有重写执行父类的。* 5、分析执行结果* a1.show(b)a1没有多态引用直接找A类的方法b是B类对象只能选择public String show(A obj) A and A* a2.show(d)a2多态引用执行子类的方法d是D类对象但是因为此时编译时按A类编译所以在编译期间先确定是调用* public String show(A obj)而后执行子类重写的public String show(A obj) B and A* 而不是直接选最合适的public String show(B obj)* b.show(c)b没有多态引用直接找B类的方法c是C类的对象选择最合适的public String show(C obj) A and C* b.show(d)b没有多态引用直接找B类的方法d是D类对象选最合适的public String show(B obj) B and B*/
public class Exercise30 {public static void main(String[] args) {A a1 new A();A a2 new B();B b new B();C c new C();D d new D();System.out.println((1) a1.show(b));System.out.println((2) a2.show(d));System.out.println((3) b.show(c));System.out.println((4) b.show(d));}
}class A {public String show(C obj) {return (A and C);}public String show(A obj) {return A and A;}
}class B extends A {public String show(B obj) {return B and B;}public String show(A obj) {return B and A;}
}class C extends B {}class D extends B {}31、判断运行结果
考核点属性与多态无关
public class Exercise31 {public static void main(String[] args) {Base b new Sub();System.out.println(b.x);}
}
class Base{int x 1;
}
class Sub extends Base{int x 2;
}/** 属性没有多态性只看编译时类型*/
public class Exercise31 {public static void main(String[] args) {Base b new Sub();System.out.println(b.x);}
}
class Base{int x 1;
}
class Sub extends Base{int x 2;
}32、判断运行结果
/*
下面这些代码调用的都是成员变量不是虚方法。
只看变量或对象的“编译时类型”就可以了。
什么是编译时类型
1对于变量来说变量声明时左边的类型就是它的编译时类型
2对于强制类型转换来说()中写的类型是什么它的编译时类型就是什么。如果有连续多次的强制类型转换看最后一次。*/
public class Exercise32 {public static void main(String[] args) {A a new B();System.out.println(a.num);//a变量的编译时类型是A//1System.out.println(((B)a).num);//((B)a)的编译时类型就是B//2System.out.println(((A)((B)a)).num);//((A)((B)a))最后的类型是(A)它的编译时类型就是A//1System.out.println(-------------------);B b new B();System.out.println(b.num);//b变量的编译时类型是B//2System.out.println(((A)b).num);//((A)b)编译时类型是A//1System.out.println(((B)((A)b)).num);//((B)((A)b))编译时类型B//2}
}
class A{int num 1;
}
class B extends A{int num 2;
}06-Object类
33、Triangle类
声明三角形类Triangle
1属性double类型的a,b,c代表三角形的三条边要求属性使用final声明并且私有化
2提供有参构造并且在构造器中检查a,b,c是否可以构成三角形如果a,b,c可以构成三角形则正常赋值否则提示错误并且a,b,c赋值为0
3生成a,b,c的get方法
4重写toString方法并返回三角形信息例如“三角形三边是3.0,4.0,5.0面积是6.0周长是12.0”
5重写hashCode和equals方法
6编写 public double getArea()求面积方法
7编写 public double getPiremeter()求周长方法
在测试类的main中创建两个三角形对象a,b,c分为赋值为3,4,5调用equals方法比较两个对象是否相等打印两个对象看结果。
public class Triangle {private final double a;private final double b;private final double c;public Triangle(double a, double b, double c) {if(a0 || b0 || c0 || abc || acb || bca){System.out.println(a , b , c 无法构成三角形);this.a 0;this.b 0;this.c 0;}else{this.a a;this.b b;this.c c;}}public double getA() {return a;}public double getB() {return b;}public double getC() {return c;}public double area(){double p (abc)/2;return Math.sqrt(p*(p-a)*(p-b)*(p-c));}public double perimeter(){return abc;}Overridepublic String toString(){return 三角形三边是 a , b , c 面积是 area() 周长是 perimeter();}Overridepublic boolean equals(Object o) {if (this o) return true;if (o null || getClass() ! o.getClass()) return false;Triangle triangle (Triangle) o;return Double.compare(triangle.a, a) 0 Double.compare(triangle.b, b) 0 Double.compare(triangle.c, c) 0;}Overridepublic int hashCode() {return Objects.hash(a, b, c);}
}
public class Exercise33 {public static void main(String[] args) {Triangle t1 new Triangle(3,4,5);Triangle t2 new Triangle(3,4,5);System.out.println(t1.equals(t2));System.out.println(t1);System.out.println(t2);}
}