面向对象编程(中级)
包
访问修饰符
封装
01:
public class Encapsulation01
{public static void main(String[] args){Person person = new Person();person.name = "Tom";person.setAge(30);person.setSalary(30000);}
}class Person
{public String name;private int age;private double salary;public void setSalary(double salary){this.salary = salary;}public double getSalary(){return this.salary;}public void setAge(int age){if (age >= 1 && age <= 123)this.age = age;else {System.out.println("Input Error");this.age = 18;//给个默认age}}public int getAge(){return this.age;}public void setName(String name){if (name.length() >= 2 && name.length() <= 6)this.name = name;else{System.out.println("Input Error");this.name = "wuming";}}public String getName(){return this.name;}public String printElem(){return "name = "+name+" age = "+age+" salary = "+salary;}}
01:
public class Encapsulation01
{public static void main(String[] args){Person person = new Person();person.name = "Tom";person.setAge(30);person.setSalary(30000);}
}class Person
{public String name;private int age;private double salary;public Person(){}public Person(String name,int age,double salary){//this.name = name;// this.age = age;// this.salary = salary;setName(name);setAge(age);setSalary(salary);}public void setSalary(double salary){this.salary = salary;}public double getSalary(){return this.salary;}public void setAge(int age){if (age >= 1 && age <= 123)this.age = name;else {System.out.println("Input Error");this.age = 18;//给个默认age}}public int getAge(){return this.age;}public void setName(String name){if (name.length() >= 2 && name.length() <= 6)this.name = name;else{System.out.println("Input Error");this.name = "wuming";}}public String getName(){return this.name;}public String printElem(){return "name = "+name+" age = "+age+" salary = "+salary;}}
小练习
package HelloDemo;public class Account {private String name;private double balance;private String password;public Account(){}public Account(String name, double balance, String password) {this.setBalance(balance);this.setName(name);this.setPassword(password);}public String getName() {return name;}public void setName(String name) {if(name.length() >= 2 && name.length() <= 4)this.name = name;else{System.out.println("Input Error");this.name = "wuming";}}public double getBalance() {return balance;}public void setBalance(double balance) {if (balance > 20)this.balance = balance;else {System.out.println("Input Error,balance = 0");}}public String getPassword() {return password;}public void setPassword(String password) {if (password.length()==6)this.password = password;else{System.out.println("Input Error password = 000000");this.password = "000000";}}public void showInfo(){//可以增加权限的校验System.out.println("name = "+name+" balance = "+balance+" password = "+password);}
}//-------------------------------------------------------------------------------package HelloDemo;public class TestAccount {public static void main(String[] args){Account account = new Account();account.setName("Tom");account.setBalance(60);account.setPassword("123456");account.showInfo();}
}
继承
01:
package JiCWorkDemo;public class Pupil {public String name;public int age;private double score;public void setScore(double score) {this.score = score;}public void testing(){System.out.println("pupil name= "+name);}public void showInfo(){System.out.println("name = "+name+" age = "+age+" score = "+score);}
}//===========================================package JiCWorkDemo;public class Graduate extends Pupil{public void testing(){System.out.println("Graduate name= "+name);}
}//===========================================package JiCWorkDemo;public class WorkDemo {public static void main(String[] args){Pupil pupil = new Pupil();pupil.name = "Tom";pupil.age = 11;pupil.testing();pupil.setScore(50);pupil.showInfo();System.out.println("========");Graduate graduate = new Graduate();graduate.name = "Jack";graduate.age = 23;graduate.testing();graduate.setScore(80);graduate.showInfo();}
}
继承的深入讨论/细节问题
01:
package JiCWorkDemo;public class Base {public int n1 = 100;protected int n2 = 200;int n3 = 300;private int n4 = 400;public int getN4(){return n4;}public void callTest400(){test400();}public Base(){System.out.println("Base()......");}public void test100(){System.out.println("test100");}protected void test200(){System.out.println("test200");}void test300(){System.out.println("test300");}private void test400(){System.out.println("test400");}}//==============================================package JiCWorkDemo;public class Son extends Base{public Son(){System.out.println("sub()...");}public void say0k(){System.out.println(n1+n2+n3);test100();test200();test300();//System.out.println(n4);//Error//test400();//ErrorSystem.out.println("n4 = "+getN4());callTest400();}
}
继承的本质分析
如果查找过程中,父类的age是private,而爷爷类的age是public,但还是会报错,因为找到父类的age是private以后就会报错,不会去找爷爷类的age
小练习
结果如下:
我是A类
hahah我是B类的有参构造
我是c类的有参构造
我是c类的无参构造
01:
package ExerciseDemoWork;public class Computer {private String cpu;private int memory;private int disk;public Computer(String cpu, int memory, int disk) {this.cpu = cpu;this.memory = memory;this.disk = disk;}public String getDetails(){return "cpu = "+cpu+" memory = "+memory+" disk = "+disk;}public String getCpu() {return cpu;}public void setCpu(String cpu) {this.cpu = cpu;}public int getMemory() {return memory;}public void setMemory(int memory) {this.memory = memory;}public int getDisk() {return disk;}public void setDisk(int disk) {this.disk = disk;}}//============================================================================package ExerciseDemoWork;public class PC extends Computer{private String brand;public PC(String cpu, int memory, int disk, String brand) {super(cpu, memory, disk);this.brand = brand;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public void showInfo(){System.out.println("Pc = ");System.out.println(getDetails()+"brand = "+brand);}
}//===========================================================================package ExerciseDemoWork;public class NotePad extends Computer{private String color;public NotePad(String cpu, int memory, int disk, String color) {super(cpu, memory, disk);this.color = color;}
}//=============================================================================package ExerciseDemoWork;public class Test {public static void main(String[] args){PC pc = new PC("intel", 16, 500, "IBM");pc.showInfo();}
}
super关键字
01:
package SuperWorkDemo;public class B extends A{public void hi() {System.out.println(super.n1 + " " +super.n2 + " " + super.n3);//System.out.println(super.n4);//Error}public void ok(){super.test100();super.test200();super.test300();//super.test400();//Error}public B(){super("jack");}// public void hello()
// {
// //super();//error
// }}//==========================================================================package SuperWorkDemo;public class A {public int n1 = 100;protected int n2 = 200;int n3 = 300;private int n4 = 400;public void test100(){};protected void test200(){};void test300(){};private void test400(){};public A(){}public A(String str){}
}
细节
方法重写/覆盖
01:
package OverrideWorkDemo;public class Animal {public void cry(){System.out.println("动物叫唤");}}//=============================================================package OverrideWorkDemo;public class Dog extends Animal{public void cry(){System.out.println("Small dog crying");}
}//==================================================================package OverrideWorkDemo;public class Override01 {public static void main(String[] args){Dog dog = new Dog();dog.cry();}
}
注意事项和使用细节
小练习
01:
package PersonWorkDemo;public class Person {private String name;private int age;public String say(){return "name = "+name+" age = "+age;}public Person(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;}}//====================================package PersonWorkDemo;public class Student extends Person{private int id;private double score;public Student(String name, int age, int id, double score) {super(name, age);this.id = id;this.score = score;}public String say(){return super.say()+" id = "+id+" score = "+score;}public int getId() {return id;}public void setId(int id) {this.id = id;}public double getScore() {return score;}public void setScore(double score) {this.score = score;}
}//=================================================package PersonWorkDemo;public class OverrideExercise {public static void main(String[] args){Person jack = new Person("jack", 10);System.out.println(jack.say());Student smith = new Student("smith",20,123456,99.8);System.out.println(smith.say());}// name = jack age = 10
// name = smith age = 20 id = 123456 score = 99.8
}
多态
01:
package Animal;public class PolyObject {public static void main(String[] args){Animal animal = new Dog();animal.cry();animal = new Cat();animal.cry();}
}//=====================================================================package Animal;public class Dog extends Animal{@Overridepublic void cry() {System.out.println("Dog cry cry......");}
}//===================================================================package Animal;public class Cat extends Animal{@Overridepublic void cry() {System.out.println("Cat cry cry....." );}}//=================================================================package Animal;public class Animal {public void cry(){System.out.println("Animal cry...");}}
多态注意事项和细节讨论
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8qOKwsbP-1634378483141)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210913233928561.png)]
01:
public class PolyDetail
{public static void main(String[] args){Animal animal = new Cat();Object obj = new Cat();System.out.println("ok~~");}
}
- 可以调用父类中的所有成员(需遵守访问权限),但是不能调用子类的特有的成员,因为在编译阶段,能调用哪些成员,是由编译类型来决定的
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0KKbSFqi-1634378483142)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210913233955641.png)]
多态的向下转型
01:
Animal animal = new Cat();Cat cat = (Cat)animal;
cat.catchMouse();
02:
Animal animal = new Cat();Dog dog = (Dog)animal;//Error
01:
package PolyDetailDemo;public class PolyDetailedDemo {public static void main(String[] args){Base base = new Sub();System.out.println(base.count);//10Sub sub = new Sub();System.out.println(sub.count);//20}}class Base
{int count = 10;
}class Sub extends Base{int count = 20;
}
02:
public class PolyDetail03
{public static void main(String[] args){BB bb = new BB();System.out.println(bb instanceof BB);//trueSystem.out.println(bb instanceof AA);//trueAA aa = new BB();System.out.println(aa instanceof AA);//trueSystem.out.println(aa instanceof BB);//trueObject obj = new Object();System.out.println(obj instanceof AA);//falseString str = "hello";System.out.println(str instanceof Object);//true}
}class AA{}class BB extends AA{}
小练习
01:
public class PolyExercise01
{public static void main(String[] args){double d = 13.4;//oklong l = (long)d;//okSystem.out.println(l);//13int in = 5;//okboolean b = (boolean)in;//error int -> booleanObject obj = "Hello";//ok 向上转型String objStr = (String)obj;//ok 向下转型System.out.println(objStr);//helloObject objPri = new Integer(5);//ok 向上转型String str = (String)objPri;//Error 指向Integer的父类引用,转成StringInteger str1 = (Integer)objPri;//ok 向下转型}
}
02:
package PolyDemoWork;public class Base {int cnt = 10;public void display(){System.out.println(this.cnt);}
}class Sub extends Base{int cnt = 20;public void display(){System.out.println(this.cnt);}}//==============================================================================package PolyDemoWork;public class PolyDemo {public static void main(String[] args){Sub s = new Sub();System.out.println(s.cnt);//20s.display();//20Base b = s;System.out.println(b==s);//trueSystem.out.println(b.cnt);//10b.display();//20}
}
java的动态绑定机制
01:
package PolyDemoWork02;public class A {public int i = 10;public int sum(){return get()+10;}public int sum1(){return i+10;}public int get(){return i;}}//====================================================================================package PolyDemoWork02;public class B extends A{public int i = 20;public int sum(){return i+20;}public int get(){return i;}public int sum1(){return i+10;}
}//=========================================================================================package PolyDemoWork02;public class PolyMainDemo {public static void main(String[] args) {A a = new B();System.out.println(a.sum());//40System.out.println(a.sum1());//30}
}
02:
package PolyDemoWork02;public class B extends A{public int i = 20;
// public int sum()
// {
// return i+20;
// }public int get(){return i;}public int sum1(){return i+10;}
}//====================================================================package PolyDemoWork02;public class A {public int i = 10;public int sum(){return get()+10;}public int sum1(){return i+10;}public int get(){return i;}}//==================================================================================package PolyDemoWork02;public class PolyMainDemo {public static void main(String[] args) {A a = new B();System.out.println(a.sum());//30System.out.println(a.sum1());//30}
}
03:
package PolyDemoWork02;public class B extends A{public int i = 20;
// public int sum()
// {
// return i+20;
// }public int get(){return i;}// public int sum1()
// {
// return i+10;
// }
}//====================================================================================package PolyDemoWork02;public class A {public int i = 10;public int sum(){return get()+10;}public int sum1(){return i+10;}public int get(){return i;}}//==============================================================================package PolyDemoWork02;public class PolyMainDemo {public static void main(String[] args) {A a = new B();System.out.println(a.sum());//30System.out.println(a.sum1());//20}
}
多态的应用
多态数组
01:
package Person;public class Person {private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}public String say(){return name+" "+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;}
}//=========================================================package Person;public class Student extends Person{private double score;public Student(String name, int age, double socre) {super(name, age);this.score = socre;}public double getSocre() {return score;}public void setSocre(double socre) {this.score = socre;}@Overridepublic String say() {return super.say()+" score = "+score;}
}//==========================================================================package Person;public class Teacher extends Person{private double sal;public Teacher(String name, int age, double sal) {super(name, age);this.sal = sal;}public double getSal() {return sal;}public void setSal(double sal) {this.sal = sal;}@Overridepublic String say() {return super.say()+" sal = "+sal;}
}//==========================================================================package Person;public class PolyArray {public static void main(String[] args){Person[] persons = new Person[3];persons[0] = new Person("Tom",20);persons[1] = new Student("Jack",30,100);persons[2] = new Teacher("Bob",33,4500);for (int i = 0;i<persons.length;i++){persons[i].say();}}
}
02:
package Person;public class Person {private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}public String say(){return name+" "+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;}
}//=================================================================================package Person;public class Student extends Person{private double score;public Student(String name, int age, double socre) {super(name, age);this.score = socre;}public double getSocre() {return score;}public void setSocre(double socre) {this.score = socre;}@Overridepublic String say() {return super.say()+" score = "+score;}public void study(){System.out.println("student = "+getName());}
}//=========================================================================package Person;public class Teacher extends Person{private double sal;public Teacher(String name, int age, double sal) {super(name, age);this.sal = sal;}public double getSal() {return sal;}public void setSal(double sal) {this.sal = sal;}@Overridepublic String say() {return super.say()+" sal = "+sal;}public void teach(){System.out.println("teacher = "+getName());}
}//=============================================================================package Person;public class PolyArray {public static void main(String[] args){Person[] persons = new Person[3];persons[0] = new Person("Tom",20);persons[1] = new Student("Jack",30,100);persons[2] = new Teacher("Bob",33,4500);for (int i = 0;i<persons.length;i++){if (persons[i] instanceof Student){((Student)persons[i]).study();}else if (persons[i] instanceof Teacher){((Teacher)persons[i]).teach();}else if (persons[i] instanceof Person){}else{System.out.println("Error");}}}
}
多态参数
01:
package TestPolyDemo;public class Employee {private String name;private double salary;public Employee(String name, double salary) {this.name = name;this.salary = salary;}public double getAnnual(){return 12*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;}}//=====================================================================package TestPolyDemo;public class Worker extends Employee{public Worker(String name, double salary) {super(name, salary);}public void work(){System.out.println("worker = "+getName()+"working!!!");}@Overridepublic double getAnnual() {return super.getAnnual();}
}//==============================================================================package TestPolyDemo;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("manager" + getName()+"is managing");}@Overridepublic double getAnnual() {return super.getAnnual()+bonus;}
}//======================================================================package TestPolyDemo;public class TestWorkDemo {public static void main(String[] args){Worker tom = new Worker("tom",2500);Manager milan = new Manager("milan", 5000, 200000);TestWorkDemo testWorkDemo = new TestWorkDemo();testWorkDemo.showEmpAnnual(tom);testWorkDemo.showEmpAnnual(milan);testWorkDemo.testWork(tom);testWorkDemo.testWork(milan);}public void showEmpAnnual(Employee e){System.out.println(e.getAnnual());}public void testWork(Employee e){if (e instanceof Worker){((Worker) e).work();}else if (e instanceof Manager){((Manager) e).manage();}else{System.out.println("no");}}}
Object类
==运算符和equals
01:
package ObjectDemo;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 obj = a;System.out.println(obj==c);//true 地址还是一样"hello".equals("abc");//String equals
// public boolean equals(Object anObject) {
// if (this == anObject) {
// return true;
// }
// if (anObject instanceof String) {
// String aString = (String)anObject;
// if (!COMPACT_STRINGS || this.coder == aString.coder) {
// return StringLatin1.equals(value, aString.value);
// }
// }
// return false;// }//Object equals// public boolean equals(Object obj) {
// return (this == obj);
// }Integer integer1 = new Integer(1000);Integer integer2 = new Integer(1000);System.out.println(integer1 == integer2);//falseSystem.out.println(integer1.equals(integer2));//trueString str1 = new String("hspedu");String str2 = new String("hspedu");System.out.println(str1==str2);//falseSystem.out.println(str1.equals(str2));//true// Integer equals// public boolean equals(Object obj) {
// if (obj instanceof Integer) {
// return value == ((Integer)obj).intValue();
// }
// return false;// }}
}class B{}class A extends B
{}
如何重写equals方法
01:
package EqualsExercise01;public class EqualsExercise01 {public static void main(String[] args){Person person1 = new Person("jack", 10, '男');Person person2 = new Person("jack", 10, '男');System.out.println(person1.equals(person2));}
}class Person
{private String name;private int age;private char gender;public boolean equals(Object obj){if (this==obj){return true;}if (obj instanceof Person){Person p = (Person)obj;return this.name.equals(p.name) && this.age== p.age && this.gender ==p.gender;}return false;}public Person(String name, int age, char gender) {this.name = name;this.age = age;this.gender = gender;}
}
小练习
01:
02:
public class WorkDemo {public static void main(String[] args) {int it = 65;float fl = 65.0f;System.out.println("65和65.0f是否相等?"+(it==fl));//truechar ch1 = 'A';char ch2 = 12;System.out.println("65和'A'是否相等?"+(it==ch1));//trueSystem.out.println("12和ch2是否相等?"+(12==ch2));//trueString str1 = new String("hello");String str2 = new String("hello");System.out.println((str1==str2));//falseSystem.out.println(str1.equals(str2));//true}
}
hashCode方法
toString方法
01:
package ObjectWorkDemo;public class TestDemo02 {public static void main(String[] args) {// Object的toString()源码
// public String toString() {
// return getClass().getName() + "@" + Integer.toHexString(hashCode());
// }Monster monster = new Monster("Tom", "climb", 1000);System.out.println(monster.toString()+" "+monster.hashCode());//ObjectWorkDemo.Monster@776ec8df 2003749087//Monster{name='Tom', job='climb', sal=1000.0} 1072408673System.out.println(monster);//Monster{name='Tom', job='climb', sal=1000.0}}}class Monster
{private String name;private String job;private double sal;public Monster(String name, String job, double sal) {this.name = name;this.job = job;this.sal = sal;}@Overridepublic String toString() {return "Monster{" +"name='" + name + '\'' +", job='" + job + '\'' +", sal=" + sal +'}';}
}
finalize方法
01:
package FinalizeDemo;public class FinalizeDemo {public static void main(String[] args){Car Bwm = new Car("Baoma");Bwm = null;System.gc();}
}class Car
{private String name;public Car(String name) {this.name = name;}@Overrideprotected void finalize() throws Throwable {System.out.println("Car destory!");}
}
大练习
01:
package HomeWorkDemo;public class HomeWorkDemo01 {public static void main(String[] args){Person[] persons = new Person[3];persons[0] = new Person("Tom",35,"JavaEE工程师");persons[1] = new Person("jack",18,"C++工程师");persons[2] = new Person("mike",89,"Teacher");for (int i = 0;i<persons.length-1;i++){for (int j = 0;j<persons.length-1-i;j++){if (persons[j].age < persons[j+1].age){Person tmp = persons[j];persons[j] = persons[j+1];persons[j+1] = tmp;}}}for (int i = 0;i<persons.length;i++){System.out.println(persons[i]);}}
}class Person
{private String name;public int age;private String job;public Person(String name, int age, String job) {this.name = name;this.age = age;this.job = job;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +", job='" + job + '\'' +'}';}
}
02:
03:
package HomeWorkDemo01;public class Teacher{private String name;private int age;private String post;private double salary;private double grade;public Teacher(String name, int age, String post, double salary, double grade) {this.name = name;this.age = age;this.post = post;this.salary = salary;this.grade = grade;}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 double getGrade() {return grade;}public void setGrade(double grade) {this.grade = grade;}public void introduce(){System.out.println(toString());}@Overridepublic String toString() {return "Teacher{" +"name='" + name + '\'' +", age=" + age +", post='" + post + '\'' +", salary=" + salary +", grade=" + grade +'}';}}//=================================================package HomeWorkDemo01;public class Professor extends Teacher{public Professor(String name, int age, String post, double salary, double grade) {super(name, age, post, salary, grade);}@Overridepublic void introduce() {System.out.println("this is professor");super.introduce();}
}//===================================================package HomeWorkDemo01;public class HomeWork03 {public static void main(String[] args){Professor professor = new Professor("贾宝玉", 30, "高级职称", 30000, 1.3);professor.introduce();}
}
04:
package HomeWork04;public class Employee {private String name;private double daySal;private int workDays;private double grade;public void printSal(){System.out.println(name+" Sal = "+daySal*workDays*grade);}public Employee(String name, double daySal, int workDays, double grade) {this.name = name;this.daySal = daySal;this.workDays = workDays;this.grade = grade;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getDaySal() {return daySal;}public void setDaySal(double daySal) {this.daySal = daySal;}public int getWorkDays() {return workDays;}public void setWorkDays(int workDays) {this.workDays = workDays;}public double getGrade() {return grade;}public void setGrade(double grade) {this.grade = grade;}
}//==================================================package HomeWork04;public class Manager extends Employee{private double bonus;public Manager(String name, double daySal, int workDays, double grade) {super(name, daySal, workDays, grade);}@Overridepublic void printSal() {System.out.println("name"+getName()+" Sal = "+(bonus+getDaySal()*getWorkDays()*getGrade()));}public double getBonus() {return bonus;}public void setBonus(double bonus) {this.bonus = bonus;}
}//==================================================package HomeWork04;public class Worker extends Employee{public Worker(String name, double daySal, int workDays, double grade) {super(name, daySal, workDays, grade);}@Overridepublic void printSal() {super.printSal();}
}//==================================================package HomeWork04;public class HomeWork04 {public static void main(String[] args){Manager manage= new Manager("刘备", 100, 20, 1.2);manage.setBonus(3000);manage.printSal();Worker worker = new Worker("关羽",50,10,1.0);worker.printSal();}}
05:
package HomeWork05;public class Worker extends Employee{public Worker(String name, double sal) {super(name, sal);}@Overridepublic void printSal() {System.out.println("Worker = ");super.printSal();}
}//==============================================package HomeWork05;public class Employee {private String name;private double sal;private int salMonth = 12;public Employee(String name, double sal) {this.name = name;this.sal = sal;}public void printSal(){System.out.println(name+" Sal = "+(sal*salMonth));}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getSal() {return sal;}public void setSal(double sal) {this.sal = sal;}public int getSalMonth() {return salMonth;}public void setSalMonth(int salMonth) {this.salMonth = salMonth;}
}//============================================package HomeWork05;public class Peasant extends Employee{public Peasant(String name, double sal) {super(name, sal);}@Overridepublic void printSal() {System.out.println("Peasant = ");super.printSal();}
}//============================================package HomeWork05;public class Teacher extends Employee{private int classDays;private double classSal;@Overridepublic void printSal() {System.out.println("name = "+getName()+" Sal = "+(getSal()*getSalMonth()+classDays * classSal));}public Teacher(String name, double sal) {super(name, sal);}public int getClassDays() {return classDays;}public void setClassDays(int classDays) {this.classDays = classDays;}public double getClassSal() {return classSal;}public void setClassSal(double classSal) {this.classSal = classSal;}
}//===============================================package HomeWork05;public class Scientist extends Employee{private double bonus;public Scientist(String name, double sal) {super(name, sal);}@Overridepublic void printSal() {System.out.println("Scientist = "+getName()+" Sal = "+getSal()*getSalMonth()+bonus);}public double getBonus() {return bonus;}public void setBonus(double bonus) {this.bonus = bonus;}
}//============================================package HomeWork05;public class HomeWork05 {public static void main(String[] args){Worker jack = new Worker("jack",1000);jack.printSal();Peasant smith = new Peasant("smith", 20000);smith.printSal();Teacher teacher = new Teacher("顺平", 2000);teacher.setClassDays(360);teacher.setClassSal(5000);teacher.printSal();Scientist scientist = new Scientist("钟南山", 20000);scientist.setBonus(2000000);scientist.printSal();}
}
06:
07:
package HomeWork07;public class Test {String name = "Rose";Test(){System.out.println("Test");}Test(String name){this.name = name;}}class Demo extends Test{String name = "jack";Demo(){super();System.out.println("Demo");}Demo(String s){super(s);}public void test(){System.out.println(super.name);System.out.println(this.name);}public static void main(String [] args){new Demo().test();new Demo("join").test();}}// Test
// Demo
// Rose
// jack
// join
// jack
08: