reminder:
This is a long article. Non combatants, please retreat...
1, Package
What is the bag?
Essence of package
The essence of a package is actually to create different folders to hold class files
Common java packages
Use of packages
2, Access modifier
What is the access modifier?
Access scope (key points)
3, Encapsulation
What is encapsulation?
Encapsulation is to encapsulate abstract attributes and methods. Data is protected internally. Other parts of the program can only be accessed through authorized methods.
Benefits of encapsulation:
1) Hide implementation details
2) Data can be verified to ensure reasonable security
Packaging steps
4, Inheritance
What is inheritance?
Use of inheritance
1) Inheritance improves code reusability
2) Code scalability and maintainability improved
Usage details:
1) Subclasses inherit all properties and methods, but private properties and methods cannot be directly methods in subclasses. They must be accessed through public methods provided by the parent class
2) The subclass must call the constructor of the parent class to complete the initialization of the parent class
3) When creating a subclass object, no matter which constructor of the subclass is used, the parameterless constructor of the parent class is always called by default. If the parent class does not provide a parameterless constructor, you must use super in the constructor of the subclass to specify which constructor of the parent class is used to complete the initialization of the parent class, otherwise the compilation fails.
4) If you want to specify which constructor of the parent class to call, explicitly call
5) super should be placed in the first line of the constructor
6) Both super() and this() can only be placed on the first line of the constructor, so the two methods cannot share a constructor
7) All classes in java are subclasses of the Object class
8) The calling of the parent class constructor is not limited to the direct parent class, and will always trace back to the top-level parent class (Object class)
9) A subclass can inherit at most one parent class
10) Inheritance cannot be abused. The is-a relationship must be satisfied
For example: Person is a Music ×
Cat is a Anaimal √
The nature of inheritance
Exercise 1:
/** * @author: Programmer flying * @description: */ public class A { A(){ System.out.println("a"); } A(String name){ System.out.println("a+name"); } } /** * @author: Programmer flying * @description: */ public class B extends A{ B(){ this("abc"); System.out.println("b"); } B(String name){ System.out.println("b+name"); } public static void main(String[] args) { B b = new B(); } }
Output:
a
b+name
b
5, super keyword
1. What is the super keyword?
super represents the reference of the parent class, which is used to access the properties, methods and constructors of the parent class
2. basic grammar
use
Comparison between super and this
6, Method override
What is rewriting?
A subclass that has the same methods as its parent (return value type, method name, parameters) is called an override.
Method override details
7, Polymorphism
The third feature of object-oriented is that methods and objects have multiple forms. Polymorphism is based on encapsulation and inheritance.
Polymorphism of methods
Overloading and rewriting reflect the polymorphism of methods
Polymorphism of objects (key points and difficulties)
1) Object polymorphism cases:
/** * @author: Programmer flying * @description:Master human */ public class Master { private String name; public Master(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void feed(Animal animal,Food food){ System.out.println("master" + name + "to" + animal.getName() + "feed" + food.getName()); } } /** * @author: Programmer flying * @description:Animal parent */ public class Animal { private String name; public Animal(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } /** * @author: Programmer flying * @description:Dog subclass */ public class Dog extends Animal{ public Dog(String name) { super(name); } } /** * @author: Programmer flying * @description:Feline subclass */ public class Cat extends Animal{ public Cat(String name) { super(name); } } /** * @author: Programmer flying * @description:Food parent */ public class Food { private String name; public Food(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } /** * @author: Programmer flying * @description:Caviar */ public class Fish extends Food{ public Fish(String name) { super(name); } } /** * @author: Programmer flying * @description:Bone subclass */ public class Bone extends Food{ public Bone(String name) { super(name); } }
Test:
/** * @author: Programmer flying * @description:Object polymorphism test */ public class Poly01 { public static void main(String[] args) { Master master = new Master("Xiao Wang"); Animal animal = new Dog("Erha"); Food food = new Bone("bone"); master.feed(animal,food); Master master2 = new Master("Xiao Wang"); Animal animal2 = new Cat("Meow meow"); Food food2 = new Bone("Yellow croaker"); master2.feed(animal2,food2); } }
Output:
Master Xiao Wang feeds erha bones
Owner Xiao Wang feeds yellow croaker to Miaomiao
2) Precautions for polymorphic use
Upward Transformation:
/** * @author: Programmer flying * @description:Animal parent */ public class Animal { String name="animal"; int age = 10; public void eat(){ System.out.println("eat"); } } /** * @author: Programmer flying * @description:Feline subclass */ public class Cat extends Animal{ public void eat(){ System.out.println("Cats eat fish"); } public void catchMouse(){ System.out.println("Cat catches mouse"); } } /** * @author: Programmer flying * @description:Polymorphic considerations: upward transformation */ public class PolyDetail { public static void main(String[] args) { //Upward transformation Animal animal = new Cat(); animal.eat(); System.out.println("ok..."); } }
Output:
Cats eat fish
ok...
Downward Transformation:
/** * @author: Programmer flying * @description:Polymorphic considerations: downward transformation */ public class PolyDetail { public static void main(String[] args) { //Downward transformation Animal animal = new Cat(); Cat cat = (Cat)animal; cat.catchMouse(); //Dog dog = (Dog)animal;// Exception thrown at runtime: ClassCastException } }
Output:
Cat catches mouse
Property override
Polymorphic exercise 1:
Polymorphic exercise 2:
/** * @author: Programmer flying * @description: */ public class Base { int count = 20; public void display(){ System.out.println(this.count); } } /** * @author: Programmer flying * @description: */ public class Sub extends Base{ int count = 10; public void display(){ System.out.println(this.count); } } /** * @author: Programmer flying * @description: */ public class PolyTest2 { public static void main(String[] args) { Sub s = new Sub(); System.out.println(s.count);//10 s.display();//10 Base b = s; System.out.println(b == s);//true System.out.println(b.count);//20 b.display();//10 } }
Dynamic binding mechanism
Polymorphic array
Application case:
Polymorphic array I:
/** * @author: Programmer flying * @description: */ public class Person { private String name; private int 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; } public String say(){ return name + "\t" +age; } } /** * @author: Programmer flying * @description: */ public class Teacher extends Person{ private double salay; public Teacher(String name, int age,double salay) { super(name, age); this.salay = salay; } public double getSalay() { return salay; } public void setSalay(double salay) { this.salay = salay; } @Override public String say() { return super.say() + "\t" + "salay:" + salay; } } /** * @author: Programmer flying * @description: */ public class Student extends Person{ private double score; public Student(String name, int age,double score) { super(name, age); this.score = score; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } @Override public String say() { return super.say() + "\t" + "score:" + score; } } /** * @author: Programmer flying * @description:Dynamic array test */ public class PolyArray { public static void main(String[] args) { Person[] person = new Person[3]; person[0] = new Person("Jone",99); person[1] = new Teacher("Smith",35,20000); person[2] = new Student("Jack",18,98); for (int i = 0; i < person.length; i++) { String say = person[i].say();//Dynamic binding System.out.println(say); } } }
output
Jone 99
Smith 35 salay:20000.0
Jack 18 score:98.0
Polymorphic array II:
/** * @author: Programmer flying * @description: */ public class Teacher extends Person{ private double salay; public Teacher(String name, int age,double salay) { super(name, age); this.salay = salay; } public double getSalay() { return salay; } public void setSalay(double salay) { this.salay = salay; } @Override public String say() { return super.say() + "\t" + "salay:" + salay; } public void teach(){ System.out.println("teacher" + getName() + "Teaching"); } } /** * @author: Programmer flying * @description: */ public class Student extends Person{ private double score; public Student(String name, int age,double score) { super(name, age); this.score = score; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } @Override public String say() { return super.say() + "\t" + "score:" + score; } public void study(){ System.out.println("student" + getName() + "I am learning"); } } /** * @author: Programmer flying * @description:Dynamic array upgrade test */ public class PolyArray { public static void main(String[] args) { Person[] person = new Person[3]; person[0] = new Person("Jone",99); person[1] = new Teacher("Smith",35,20000); person[2] = new Student("Jack",18,98); /*for (int i = 0; i < person.length; i++) { String say = person[i].say();//Dynamic binding System.out.println(say); }*/ for (int i = 0; i < person.length; i++) { String say = person[i].say();//Dynamic binding System.out.println(say); if(person[i] instanceof Teacher){ Teacher teacher = (Teacher) person[i]; teacher.teach(); }else if(person[i] instanceof Student){ Student student = (Student)person[i]; student.study(); }else{ } } } }
Output:
Jone 99
Smith 35 salay:20000.0
Teacher Smith is teaching
Jack 18 score:98.0
Student Jack is studying
Polymorphic parameter
Application case:
Code example
/** * @author: Programmer flying * @description: */ 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 this.salary * 12; } 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; } }
/** * @author: Programmer flying * @description: */ public class Worker extends Employee{ public Worker(String name, double salary) { super(name, salary); } public void work(){ System.out.println("staff" + getName() + "at work..."); } @Override public Double getAnnual() { return super.getAnnual(); } } /** * @author: Programmer flying * @description: */ public class Manager extends Employee{ private Double bonus; public Manager(String name, double salary,double bonus) { super(name, salary); this.bonus = bonus; } public void manage(){ System.out.println("manager"+ getName() + "Managing..."); } @Override public Double getAnnual() { return super.getAnnual() + bonus; } } /** * @author: Programmer flying * @description: */ public class PolyParameterTest { public static void main(String[] args) { Employee employee = new Employee("Zhang San", 12000); Worker worker = new Worker("Xiao Wang",10000); Manager manager = new Manager("Lao Li",12000,50000); PolyParameterTest polyParameterTest = new PolyParameterTest(); polyParameterTest.showEmpAnnal(worker); polyParameterTest.showEmpAnnal(manager); polyParameterTest.testWork(worker); polyParameterTest.testWork(manager); } public void showEmpAnnal(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(); } } }
Output:
120000.0
194000.0
Employee Xiao Wang is working...
Manager Lao Li is managing...