1. Object oriented
1.1 this keyword
- this definition: when the local variable and the member variable in the code have the same name, the variables in the calling method are based on the proximity principle
- Solution: in order to avoid ambiguity when local variables and member variables have the same name, we use this keyword to directly access member variables
- The difference between using a constructor and using a setter method
Constructor: set the initial data when creating an object. It can only be initialized once
setter method: set the initial data after creating the object, which can be set multiple times
1.2 succession
1.2.1 concept of inheritance
- Inheritance definition: if a class needs to inherit from another class, use the extends keyword at this time
- Inheritance grammar
public class Subclass name extends Parent class name{ }
- Inheritance concept: the inherited class is called parent class or base class
A class that inherits its parent class is called a subclass or an extended class
Parent class: stores fields and methods common to multiple subclasses
Subclass: store its own unique fields and methods
- Note: classes in Java only support single inheritance, but support multiple inheritance (not multiple inheritance)
A parent class can have multiple subclasses
- Object class is the root class of Java language. Any class is a subclass of object, either direct subclass or indirect subclass
1.2.2 judge what subclasses can inherit
After the subclass inherits from the parent class, it can own some members (fields and methods) of the parent class. It is judged according to the access modifier:
-
If members of the parent class use public and protected modifiers, all subclasses can inherit
-
If the parent class and child class are in the same package, the default access decorated member is used, and the child class can inherit to
-
If the member in the parent class is decorated with private, the subclass cannot inherit Can only be accessed in this class
-
The constructor of the parent class and the subclass cannot inherit, because the constructor must be the same as the current class name
1.3 method coverage
When a subclass has the same method as the parent class, we say that the subclass overrides the method of the parent class, which is also called overriding
//Parent bird public class Bird { //How birds can fly public void fly() { System.out.println("fly"); } }
//Subclass ostrich public class Ostrich extends Bird { public void fly(){ System.out.println("Can't fly"); } }
//Requirements: demonstrate method coverage public class OverrideDemo { /* The parent Bird contains the behavior of fly ing Ostrich is a subclass of birds. The subclass covers the fly method of the parent class */ public static void main(String[] args){ Ostrich o = new Ostrich(); o.fly(); } }
- Method calling order: find out whether there is a corresponding method in the subclass first. If there is no corresponding method, execute the parent class, and if there is a subclass, execute the subclass. If there is no corresponding method, report an error
- Details covered by the method:
private modified methods cannot be inherited by subclasses, so they are not overwritten
-
The instance method signature must be the same (method signature = method name + method parameter list)
-
The return value type of the subclass method is the same as that of the parent method or its subclass
-
The exception thrown in the subclass method declaration is less than or equal to the exception type thrown in the parent method declaration
-
The access rights of subclass methods are greater or equal than those of parent methods
Summary: if you don't understand the above four points, you can directly copy the definition of the method in the parent class, paste it into the subclass, and then rewrite the subclass method body
1.4 super keyword
Super function: when the method of the subclass overrides the method of the parent class, we need to use super when we want the subclass to call the method of the parent class Method name ()
//Subclass ostrich public class Ostrich extends Bird { public void fly(){ System.out.println("Can't fly"); } public void say(){ fly(); super.fly(); } }
1.5 abstract methods and abstract classes
The method of abstract modification has two characteristics:
-
This method has no method body
-
Subclasses are required to override this method
Methods modified with abstract are called abstract methods
public abstract Return type method name (parameter);
characteristic:
-
Using abstract decoration, there is no method body, which is left to subclasses to cover
-
Abstract methods must be defined in abstract classes or interfaces
The class decorated with abstract becomes an abstract class
public abstract class Class name{ }
Generally, Abstract classes are prefixed with Abstract, which can be seen at a glance
characteristic:
-
Abstract classes cannot create objects, and it makes no sense to call abstract methods without method bodies
-
Abstract classes can have both abstract and ordinary methods
-
Abstract classes need subclasses to be meaningful. Subclasses must override the abstract methods of the parent class, otherwise subclasses must also be regarded as abstract classes
//Represents an abstract class for calculating shape area, which contains an abstract method for calculating area. Other subclasses must override this method public abstract class AbstractGraph { public abstract double getArea(); }
1.6 Object class and common methods
Object itself means to represent an object. It is the root class in Java. It is either the direct parent or the indirect parent of a class
class A{} Actually equivalent to class A extends Object{}
Because all classes are subclasses of Object class, all objects of class can call methods in Object class
1.6.1 equals method
The equals method and the "= =" symbol in the Object class are the same, which are used to compare whether the Object is in the same storage address
In actual development: we usually compare whether the values of member variables in two objects are equal to judge whether the two objects are the same
Official suggestion: each class should override the equals method to compare the data we care about, not the memory address.
1.6.2 toString method
Indicates that the field information in the object is converted to string format
By default, the hashCode value of the object is printed, but we are more concerned about the data stored in the field of the object
Official suggestion: every class should override toString to return the data we care about
public class Person { protected String name; protected int 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 Person() { } public Person(String name, int age) { this.name = name; this.age = age; } public boolean equals(Object o) { return true; } public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
//Requirements: demonstrate the toString and equals methods in the Object class public class ObjectDemo { //-Create a Person class and print the contents of the Person object by overriding the toString method // //-Compare two Person objects with = = and equals, and get the difference between = = and equals public static void main(String[] args){ Person p = new Person(); p.setName("week"); p.setAge(17); Person p1 = new Person(); p1.setName("week"); p1.setAge(17); boolean result1 = p == p1; boolean result2 = p.equals(p1); System.out.println(result1);//Different addresses false System.out.println(result2);//Same value true //Official toString output: com day02. demo. objectdemo. Person@2133c8f8 //Override the official toString method in Person //Person{name = 'week', age=17} System.out.println(p1.toString()); //It is used in actual development because the println method will call the toString method that passes parameters System.out.println(p1); } }
expand:
==What exactly do symbols compare
-
Compare basic data types: compare whether two values are equal
-
Compare object data type: compare whether two objects are the same memory space (address is the same)
Every time the new keyword is used, it means that a new memory space is created in the heap
2. Summary
2.1 function of this keyword
When the name of the member variable in the class is the same as that of the formal parameter, in order to avoid ambiguity (proximity principle), we use this to directly point to the member variable
2.2 setting values for member variables, selection of constructor and setter methods
Constructor: constructors can assign values while creating objects, but only once
setter method: assign a value after creating an object. You can set it multiple times
2.3 why inheritance?
When the member variables and member methods (with the same code) in multiple classes are the same, we can use inheritance to simplify the code
2.4 inheritance syntax in Java
Syntax: public class subclass name extends parent class name {}
2.5 which members can a subclass inherit from its parent class?
Both public and protected modified subclasses can inherit. When the parent and subclass are in the same package, those without modifiers can also inherit
2.6 when does a subclass need to override the methods in the parent class?
When the methods of the parent class do not match the information of the child class, we should use override
2.7 function of super keyword
Super: when the subclass overrides the method of the parent class, we need to use super if we want to call the method of the parent class Method name
2.8 when do I need to define a method in a parent class as an abstract method?
When the method of the parent class has no practical significance (no method body is required) and the child class must override the method
2.9 how should abstract classes be used? Can you create objects directly using abstract classes?
Abstract class: the name of an abstract class should be abstract, which is well-known. An abstract class must have abstract methods, and a subclass must override the abstract methods of the parent class
You cannot create objects directly using abstract classes
2.10 the role of toString() in object? How to use it in actual development?
ToString: the toString() method of the object class returns the value of "class name + @ + hashcode" of the object implementation class by default
Actual development: in actual development, we need to customize toString() to convert the field information in the object into the string format we want
2.11 what is the difference between equals() and = = in object? How to use it in actual development?
Same: when comparing objects, equals() and = = in the Object are the same to compare whether the addresses are the same
Difference: = = can be used to compare whether the values of basic data types are equal, but equals() cannot
Actual development: we need to customize the equals() method and use equals to compare whether the values in the object are equal