网站备案管局,想自己做个网站怎么做,php做的知名网站,自己注册一个网站要多少钱多态的应用
多态参数
方法定义的形参类型为父类类型#xff0c;实参类型允许为子类类型。
应用实例1#xff1a;前面的主人喂动物例子
应用实例2#xff1a;定义员工类 Employee#xff0c;包含姓名和月工资 [private] #xff0c;以及计算年工资 getAnnual 的方法。普…多态的应用
多态参数
方法定义的形参类型为父类类型实参类型允许为子类类型。
应用实例1前面的主人喂动物例子
应用实例2定义员工类 Employee包含姓名和月工资 [private] 以及计算年工资 getAnnual 的方法。普通员工和经理继承了员工经理类多了奖金 bonus 属性和管理 manage 方法普通员工类多了 work 方法普通员工和经理类要求分别重写 getAnnual 方法。
测试类中添加一个方法showEmpAnnual(Employee e)实现获取任何员工对象的年工资并在 main 方法中调用该方法[e.getAnnual()]。测试类中添加一个方法testWork如果是普通员工则调用 work 方法如果是经理则调用 manage 方法。
PolyParameter.java
package com.hspedu.poly_.polyparameter_;public class PolyParameter {public static void main(String[] args) {Worker tom new Worker(Tom, 3000);Manager john new Manager(John, 30000, 100000);PolyParameter polyParameter new PolyParameter();polyParameter.showEmpAnnual(tom);polyParameter.showEmpAnnual(john);polyParameter.testWork(tom);polyParameter.testWork(john);}public void showEmpAnnual(Employee employee){System.out.println(employee.getName() 的年工资为 employee.getAnnual());}public void testWork(Employee employee){if(employee instanceof Worker){((Worker) employee).work();//向下转型}else if(employee instanceof Manager){((Manager) employee).manage();//向下转型}else {System.out.println(输入有误...);}}
}Employee.java
package com.hspedu.poly_.polyparameter_;public class Employee {private String name;private double salary;public Employee(String name, double salary) {this.name name;this.salary salary;}public String getName() {return name;}public void setName(String name) {this.name name;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary salary;}public double getAnnual(){return 12 * salary;}
}Manager.java
package com.hspedu.poly_.polyparameter_;public class Manager extends Employee {private double bonus;public Manager(String name, double salary, double bonus) {super(name, salary);this.bonus bonus;}public double getBonus() {return bonus;}public void setBonus(double bonus) {this.bonus bonus;}public void manage(){System.out.println(经理 getName() 正在工作...);}Overridepublic double getAnnual() {return super.getAnnual() bonus;}
}Worker.java
package com.hspedu.poly_.polyparameter_;public class Worker extends Employee {public Worker(String name, double salary) {super(name, salary);}public void work(){System.out.println(普通员工 getName() 正在工作...);}Overridepublic double getAnnual() {//因为普通员工没有其他收入直接调用父类方法return super.getAnnual();}
}Object 类详解
equals 方法 和 equals 的对比面试题 是一个比较运算符。 既可以判断基本类型又可以判断引用类型。 如果判断基本类型判断的是值是否相等。eg int i 10; double d 10.0; 如果判断引用类型判断的是地址是否相等即判定是不是同一个对象。 Equals01.java package com.hspedu.object_;public class Equals01 {public static void main(String[] args) {A a new A();A b a;A c b;System.out.println(a c);//trueSystem.out.println(b c);//trueB bObj a;System.out.println(bObj c);//trueint num1 10;double num2 10.0;System.out.println(num1 num2);//true}
}
class B{}
class A extends B{}equals 是 Object 类中的方法。 只能判断引用类型。 System.out.println(hello.equals(abc));默认判断的是地址是否相等子类中往往重写该方法用于判断内容是否相等。比如IntegerString。 Integer integer1 new Integer(1000);
Integer integer2 new Integer(1000);
System.out.println(integer1 integer2);//false 因为是两个不同的对象
System.out.println(integer1.equals(integer2));//true 因为这里比较的是内容String str1 new String(hspedu);
String str2 new String(hspedu);
System.out.println(str1 str2);//false
System.out.println(str1.equals(str2));//true如何重写 equals 方法 应用实例: 判断两个 Person 对象的内容是否相等如果两个 Person 对象的各个属性值都一样则返回 true反之 false。 EqualsExercise01.java package com.hspedu.object_;public class EqualsExercise01 {public static void main(String[] args) {Person person new Person(Jack, 18, 男);Person person1 new Person(Jack, 18, 男);System.out.println(person.equals(person1));//true}
}
class Person{// 默认 extends Objectprivate String name;private int age;private char gender;// 重写 Object 的 equals 方法public boolean equals(Object obj){//削断如果比较的两个对象是同一个对象则直接返回trueif(this obj){return true;}else if (obj instanceof Person){//进行向下转型Person person (Person)obj;//需要拿到obj的构造属性return this.name.equals(person.name) this.age person.age this.gender person.gender;}else{return false;}}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;}
}练习题
第一题
class Person{public String name;
}
Person p1 new Person();
p1.name hspedu;
Person p2 new Person();
p2.name hspedu;
System.out.println(p1 p2);//false
System.out.println(p1.name.equals(p2.name));//true
System.out.println(p1.equals(p2));//false
String s1 new String(asdf);
String s2 new String(asdf);
System.out.println(s1.equals(s2));//true
System.out.println(s1 s2);//false第二题
int it 65;
float f1 65.0f;
System.out.printIn(65 和 65.0f 是否相等? (it fl));//true
char ch1 A;
char ch2 12;
System.out.println(65 和 A’ 是否相等? (it ch1));//true
System.out.println(12 和 ch2 是否相等? (12 ch2));//true
String str1 new String(hello);
String str2 new String(hello);
System.out.println( str1 和 str2 是否相等? (str1 str2))//false
System.out.println(str1 是否 equals str2? (str1.equals(str2)));//true
System.out.println(hello new java.sql.Date());//对象不同无法比较编译器报错