Overloaded function:
The function name must be the same, but the type / number of parameters of the function must be different
Cannot be distinguished by return value type
public static int add(int a, int b) { } public static int add(double a, double b) { } // Can be overloaded with the function on the first line public static double add(int a, int b) { } // Cannot be overloaded
Object and basic type:
Creating a new object is equivalent to a pointer in C language, which is called Reference
Object assignment is Reference assignment
The basic type assignment is a copy of the value
new displays the default value of an object:
After a new object is generated, if there is no assignment, there will be an internal default attribute value:
type | Default value |
---|---|
short | 0 |
int | 0 |
long | 0 |
boolean | false |
char | '\u0000' |
byte | 0 |
float | 0.0f |
double | 0.0d |
Constructor:
The constructor name must be the same as the class name and has no return value
Every Java class must have a constructor, otherwise the compiler automatically adds an empty constructor
You can have multiple constructors as long as the formal parameter list is different
Information hiding:
The member property of the class is private
Class methods are common. Modify the attribute value public through the method
this pointer:
this pointer is responsible for pointing to:
- Member variables in this class
- Member methods in this class
You can also replace the constructor in this class
super():
super() is the constructor of the parent class.
For subclasses, if the first sentence of the constructor is not super, the compiler will automatically add a super();
If the first sentence of the constructor is super written by the programmer, it will not be added automatically.
And there can only be one super in a constructor.
as
public class A{ public A(){ System.out.println("dadadadadadad"); } public A(int a){ System.out.println("I am dadadadadadad"); } } public class B extend{ public B(){ System.out.println("son son son son"); } public B(int a){ System.out.println("I am son son son son"); } public static void main(String[] a){ B obj1 = new B; System.out.println(">>>>>>>>>>>>>>>>>>>"); B obj2 = new B(10); } }
Output is:
dadadadadadad son son son son >>>>>>>>>>>>>>>>>>> dadadadadadad I am son son son son
If the class B definition is modified to:
public class B extend{ public B(){ System.out.println("son son son son"); } public B(int a){ super(a) System.out.println("I am son son son son"); } public static void main(String[] a){ B obj1 = new B; System.out.println(">>>>>>>>>>>>>>>>>>>"); B obj2 = new B(10); } }
Then the output becomes:
dadadadadadad son son son son >>>>>>>>>>>>>>>>>>> I am dadadadadadad I am son son son son
abstract class
Class: attribute (0 or more) + method (0 or more)
Complete class: all methods have implementation (method body)
Classes can have no methods, but if there are methods, experiments are needed. Such a class is a complete class, and a complete class can be instantiated: it is created by new
Abstract class: there are methods that are not implemented
Abstract classes are declared using the keyword abstract
Composition of abstract classes: member variables, concrete methods and abstract methods (declared with the keyword Abstract)
Subclasses can inherit abstract classes, but they must implement all abstract methods of the parent class. Otherwise, subclasses should also be defined as abstract classes
Interface
If all the methods of a class are not implemented, the class can be considered as an interface
public interface People { public void eat(); public void sleep(); } public interface Climb { public void climbTree(); }
Interfaces can inherit multiple interfaces, and methods that are not implemented will be superimposed.
Class implements (not inherits) an interface and must implement all methods of the interface. Otherwise, the class is defined as an abstract class
public class Man implements People { public void eat(){ System.out.println("I can eat"); } public void sleep(){ System.out.printlb("I can sleep"); } } public abstract class baby implements Climb { public void eat(){ System.out.println("I can eat"); } public void sleep(){ System.out.printlb("I can sleep"); } public abstract void climbTree(); }
Tips:
- double is a floating-point number, which is imprecise and cannot be used in switch statements
- The main method can be overloaded, called, inherited, and hidden
- The formal parameter name of the main method can be changed, but the type cannot be changed
- JDK contains JRE. JDK is the development kit and JRE is the running environment