Rewriting principle
Two are the same, two are small and one is large
*Two same: subclass method The method name is the same as that of the parameter list and parent class
*One: subclass method Method modifier Permission > = of parent method
*Two small: the return value type of the subclass method must be < = the return value type of the parent method
*Note: the < = here means that the return value type of the subclass method is the return value type of the parent class Subclass
*Or it is consistent with the return value of the parent class. If the return value type of the method of the parent class is void, the child Class can be maintained all the time*/
**** @ Override / / annotation, which is used to load the override of the method and judge whether the override is correct and can run,
package cn.tedu.oop; public class ExtendsDemo { public static void main(String[] args) { //4. Create object new Father().eat(); new Son().eat(); new Son().play(); } } //1. Create parent class class Father{ public void eat(){ System.out.println("Dad loves meat"); } public void play(){ System.out.println("Dad loves flying kites"); } } //2. Create subclasses class Son extends Father{ //5. If the subclass is not satisfied with the method of the parent class, it can override the method of the parent class /*Rewriting principle: two are the same, two are small and one is large * Two same: the method name of the subclass method is the same as that of the parameter list and the parent class method * One: Method modifier permission of subclass method > = permission of parent class method * Two small: the return value type of the subclass method must be < = the return value type of the parent method * Note: the < = here means that the return value type of the subclass method is a subclass of the return value type of the parent class * Or it is consistent with the return value of the parent class. If the return value type of the method of the parent class is void, the child class can be kept all the time*/ @Override//annotation public void eat() { System.out.println("I like vegetables"); } @Override public void play() { System.out.println("My son likes playing games"); } }
Static:
1. You can modify attributes and methods with static
2. Resources modified by static are called static
3. Static resources are loaded along with the cumulative loading. They are loaded first and take precedence over the loading of objects
4. Static resources can also be called directly through the class name
5. Static is shared by all global objects, and there is only one value
6. Static resources can only call static resources
7. this and super keywords are allowed in the static area
8. / * format: static {}*
Location: outside class method*
Execution timing: static code blocks also belong to static resources. They are loaded with class loading, which takes precedence over object loading
*And static resources are loaded only once
*Function: used to load resources that need to be loaded at the first time and can only be loaded once. It is often used for initialization*/
eg:
public static void main(String[] args) { Fruit f = new Fruit(); Fruit f2 = new Fruit(); f.grow();//No hint, you need to write it yourself Fruit.clean();//We can call directly through the class name, and the idea will prompt System.out.println(f.weight); System.out.println(Fruit.kind); System.out.println(Fruit.weight); /*2.Static resources have only one copy in memory and are shared by all global objects * All: after we modify the value of static variables in any way * No matter how you view it, the value of the static variable is the value just modified*/ Fruit.kind="Apple"; System.out.println(f.kind); System.out.println(f2.kind); System.out.println(Fruit.kind); f.kind="Banana"; System.out.println(f.kind); System.out.println(f2.kind); System.out.println(Fruit.kind); f2.kind="kiwifruit"; System.out.println(f.kind); System.out.println(f2.kind); System.out.println(Fruit.kind); } } //1. Create a fruit class /*1.Can static be used to modify attributes? Yes* *Therefore, static resources can be called directly through the class name, even if there is no missionary object*/ class Fruit{ //2. Define attributes static String kind;//varieties static double weight;//weight //3. Define common methods public static void clean(){ System.out.println("Wash fruit"); } public void grow(){ System.out.println("The fruit looks delicious~"); }
Static call relationship
*1. Ordinary resources can call either ordinary resources or static resources
*2. Static resources can only call static resources*/
eg:
public class TestStatic2 { public static void main(String[] args) { } } //1. Create a teacher class class Teacher { //2. Define the general attributes and methods of the teacher class String name; public void teach() { /*1.Can ordinary resources be adjusted to static resources-- sure*/ System.out.println("Lecture ing..."); System.out.println(id);//Normal method call static properties ready();//Normal method calls static method } //3. Define the static attributes and methods of the teacher class static int id;//Job number public static void ready() { System.out.println("Lesson preparation ing..."); } public static void eat(){ /*3.Can static resources be called-- sure*/ System.out.println(id); ready(); } }
Sequence of operations -- Static code Construction code block construction method common method local code block
eg: Test
public class TestBlock { public static void main(String[] args) { new Apple().clean(); } } //1. Create class class Apple { //6. Create a static code block of this class /*Format: static {} * Location class method *Execution timing: static code blocks also belong to static resources. They are loaded with class loading, which takes precedence over object loading * And static resources are loaded only once * Function: used to load resources that need to be loaded at the first time and can only be loaded once. It is often used for initialization*/ static { System.out.println("I am a static code block"); } //2. Create the construction code block of this class /*Location: inside class, outside method * Execution timing: the creation object is execution, and the construction method execution is given priority * Function: used to extract the common functions of all construction methods*/ { System.out.println("I am a building block"); } //3. Create a parameterless structure of this class public Apple(){ System.out.println("Nonparametric structure"); } //4. Common methods for creating this class public void clean(){ System.out.println("I am an ordinary method"); //5. Create local code blocks of this class /*Location: in method * Execution timing: the method in which the local code block is called will be executed every time * Action: used to control the action range of variables * */ { System.out.println("I am a partial code block"); } } }
final keyword
1. Modified class: the final class cannot be inherited
2. Modification method: This is the final implementation of the method and cannot be rewritten
3. Modify the constant. The value cannot be changed, and the defined constant must be assigned a value
eg:
public class TestFinal { public static void main(String[] args) { new Father2().work(); new Son2().work(); } } //1. Define parent class /*1.final Class can be modified. What is modified by final is that the final class cannot be inherited * The class modified by final can be regarded as a leaf node in the number structure*/ //3. The of the test class is modified by final //final class Father{ class Father2{ //4. Common method for defining parent class /*2.final It can be used to modify a method. The method modified by final is the final implementation of this method and cannot be overridden*/ public void work(){ System.out.println("Work in a factory~"); } } //2. Define subclasses final class Son2 extends Father2{ //5. Override the method of the parent class @Override//This annotation is used to mark that there is a long write method at this time public void work(){ /*3.Constant modified by final. The value of the constant cannot be modified * Note: final must be assigned whether in member or local variables * Note: the names of constants must be all capitalized. Words are used directly with words_ division*/ final int B=100; //B=200;// Error: constant value cannot be modified System.out.println("Work in an Internet factory~"); } }
polymorphic
Definition: refers to that the same entity has multiple forms at the same time, that is, the same object represents different objects at different times. It refers to multiple forms of objects.
Subclass objects are regarded as parent classes, so as to shield the differences between different subclass objects, write general code, make general programming and unify call standards.
characteristic
1. The premise of polymorphism is inheritance
2. The premise of polymorphism 2: there should be rewriting of methods
3. The parent class reference points to the child class object, such as: Animal a = new Cat();
4. In polymorphism, see the left for compilation and the right for operation
eg:
/*1. Polymorphic premise inheritance + rewriting*/
/*2. Objects of the parent class cannot use the unique functions of the child class*/
/*3. Formula 1: the parent class reference points to the child class object
*Explanation: the address value of the created subclass object is saved by the reference type variable of the parent type*/
/*4. Pithy formula 2 compile to the left and run to the right
*Explanation: this method must be defined in the parent class before the polymorphic object can be regarded as the parent type through compilation
* This method must be overridden in subclasses to satisfy polymorphism. Subclasses are the most important ones*/
public class TestDemo { public static void main(String[] args) { Animal a = new Animal(); Cat c = new Cat(); Dog d = new Dog(); //a.eat(); c.eat(); d.eat(); /*2.Objects of the parent class cannot use the unique functions of the child class*/ //a.jump(); //a.run(); //7. Create polymorphic objects for testing /*3.Pithy formula 1: the parent class reference points to the child class object * Explanation: the address value of the created subclass object is saved by the reference type variable of the parent type*/ Animal a2 = new Cat();//The address value of Cat class object is given to the parent type variable a2 to save Animal a3 = new Dog();//The address value of the Dog class object is given to the parent type variable a3 to save //8. Test polymorphic objects /*4.Pithy formula 2 compile to the left and run to the right * Explanation: this method must be defined in the parent class before the polymorphic object can be regarded as the parent type through compilation * This method must be overridden in subclasses to satisfy polymorphism. Subclasses are the most important ones*/ a2.eat();//Kittens like to eat dried fish ~, and polymorphic objects use the definition of the parent class and the method body of the child class } } /*1.Polymorphic premise inheritance + rewriting*/ //1. Create parent class class Animal{ //3. Common method of creating parent class public void eat(){ System.out.println("Small animals Animal You can eat anything"); } } //2.1 create subclass 1 class Cat extends Animal{ @Override public void eat(){ System.out.println("Kittens love dried fish~"); } public void jump(){ System.out.println("kitten Cat Jump high"); } } //2.2 create subclass 2 class Dog extends Animal{ @Override public void eat(){ System.out.println("Dogs love meat and bones~"); } public void run(){ System.out.println("puppy Dog Running fast"); } }
Test case:
public class TestDmoe2 { public static void main(String[] args) { //7. Create subclass objects Dog2 d=new Dog2(); // System.out.println(d.sum);//20. Subclass's own attributes //d.eat();// Puppies love to eat meat buns, subclasses their own way //8. Create polymorphic objects /*Pithy formula 1: parent reference points to subclass object * Pithy formula 2: compile (save) to the left and run (effect) to the right*/ Animal2 a = new Dog2(); /*In polymorphism, the member variable uses the parent class*/ System.out.println(a.sum);//10 /*In polymorphism, the declaration issued by Fang uses the parent class and the method body uses the child class*/ a.eat();//Dogs love meat buns /*In polymorphism, the static method of calling is a parent class, because polymorphic objects regard themselves as parent class types. * Use the static resources in the parent class directly*/ a.play();//No hint, but you can write "play anything" Animal2.play(); } } //1. Create parent class class Animal2{ int sum=10; public void eat(){ System.out.println("You can eat anything~~"); } //9.1 defining static methods of parent classes public static void play(){ System.out.println("You can play anything~"); } } //2. Create subclasses class Dog2 extends Animal2{ //5. Define member variables of subclasses int sum=20; //6. Override the method of the parent class @Override public void eat(){ System.out.println("Dogs love meat buns"); } //9.2 defining static methods of parent classes //@Override /*This is not as like as two peas, but just two static methods that appear exactly in the two classes. * Static methods belong to class resources. There is only one copy, and there is no rewriting * It is only defined in the class and used as the resource of which class*/ public static void play(){ System.out.println("Dogs like to play ball games~"); } }
eg: Car
public class DesignCar { public static void main(String[] args) { //9. Create a pure parent object for testing Car c=new Car(); System.out.println(c.getColor()); c.start(); c.stop(); //c.swim(); //10. Create pure subclass objects for testing BMW b=new BMW(); System.out.println(b.color);//Colorful black System.out.println(b.getColor()); b.start(); b.stop(); //11. Create polymorphic objects for testing Car c2=new TSL(); // Car c3=new BMW(); c2.stop(); c2.start(); c2.getColor(); } } //1. Through analysis, an automobile class is abstracted class Car{ //2. Define and encapsulate the attribute of automobile class -- member variable private String brand;//brand private String color;//colour private int id;//number private double price;//Price //3.1 creating common methods public void start(){ System.out.println("My car started!~"); } public void stop(){ System.out.println("The engine is off"); } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public int getId() { return id; } public void setId(int id) { this.id = id; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } } //4. Create subclasses class BMW extends Car{ String color="Colorful black"; @Override //5. Override parent method public void start(){ System.out.println("Let me, I'm taking off"); } } class TSL extends Car{ public void stop(){ System.out.println("ting Why don't you come down"); } //8. Add specific functions of subclasses public void swim(){ System.out.println("Unexpectedly, I'm still a submarine"); }