Object-oriented
class
-
Use the keyword class definition and have a fixed format
-
format:
Modifier class class name {
Member variable 1;
Member variable 2;
…
Member method 1;
Member method 2;
…
}
public class Person {
// 1. 属性 : 事物的特征
// 特征以成员变量的形式存在的, 就是一个普通变量定义,但是位置"类中方法外"
// 成员变量也称为全局变量
String name;
int age;
// 2. 行为 : 功能,方法
// 方法可以不使用static关键字修饰, 只使用public即可
public void eat(){
System.out.println(name + "正在吃饭");
}
public void sleep(){
System.out.println(name + "沉睡了" + age + "年");
}
Use of objects
-
Create object: class name object name = new class name(); // class itself is a reference data type
-
Use of members:
Get member variable value:
Variable = object name. Member variable;
Modify member variable value:
对象名.成员变量 = 新值;
Access member method:
Object name. Member method (actual parameter);
public static void main(String[] args) { // 1. 创建出一个Person类型对象, 表示一个具体的人类 Person p = new Person(); // 2. 使用对象名p.调用成员变量和方法功能 // 1) 给对象p中成员变量进行赋值 p.name = "张三"; p.age = 20; // 2) 调用eat和sleep方法功能 p.eat(); p.sleep(); // 2. 创建数第二个Person人类对象 // 注意 : 当创建出一个对象时, JVM虚拟机会为当前对象中的每一个成员变量进行默认的赋初值动作 // 根据不同的数据类型, 给不同初始值 // 整数 : 0 // 浮点 : 0.0 // 字符 : ' ' // 布尔 : false // 引用数据类型 : null Person p1 = new Person(); System.out.println(p1.name);// null System.out.println(p1.age);// 0 }
Anonymous object
- Format: new class name (actual parameter);
- Features: Can only be used once
Encapsulation
-
private: is a keyword, meaning private.
private int age;
2. The getter method and the setter method are the uniform access methods provided to the outside world after the properties are encapsulated
Shortcut key ctrl+insert
3. This keyword. Which object calls the method with the this keyword, the this keyword indicates which object itself
Construction method
Cat c = new Cat();
The parentheses are the construction method
Static
Static is static and belongs to the class
Load first
inherit
- format:
class subclass extends parent class {}
Parent class: inherited class, super class, base class
Subclass: Class used for inheritance, derived class
Example:
class Dog extends Animal { }
-
If the member variables in the sub-parent class are repeatedly defined and want to call the parent class member variables, then use the keyword super
super: keyword, which means the current type parent class reference (super is used in subclass types)
Use: super.parent class member variable name;
super represents a reference to the parent class of the current object of this class
Override
Private methods cannot be overridden
Static code block
static {
静态代码块的内容
}
1. Location: outside the class method
Polymorphism
Parent type variable name = new subclass name (actual parameter);
Person p = new Teacher();
Compile and look at the left, run and look at the left
Polymorphic downcast
Teacher tea = (Teacher)p;
Abstract method
Modifier abstract return value type method name (parameter list);
1. The class modified by the keyword abstract in java is an abstract class
public abstract class Aniaml {
public abstract void eat();
}
Template class
public abstract class ComposionTemplate {
// 书写作文的方法
public void write(){
// 书写作文的标题
System.out.println("我的哥哥");
// 作文的正文,抽象方法,待补充
body();
// 书写作文的结尾
System.out.println("啊!这就是我的哥哥");
}
interface
The keyword interface means
Modifier interface interface name {interface content}
Class implements interface
Class implements interface name 1, interface name 2... {content of the class}
Single realization:
A class implements an interface implementation
Class implements interface name {
Override all abstract methods in the interface
}
Multi-implementation:
The way a class implements multiple different interfaces at the same time is to implement multiple
Class implements interface name 1, interface name 2...{
-
Rewrite all abstract methods in all interfaces
-
The abstract method declared by the same method in different interfaces only needs to be rewritten once
}
Anonymous inner class
format:
new class name (actual parameter) {rewrite related method}
or
new interface name (actual parameter) {rewrite related method}
For example:
new Inter(){
@Override
public void method(){
}
};
final keyword
1. Final is a keyword meaning: final, final, which means that it cannot be changed.
2. Final keyword: you can modify classes, methods, and variables
Object class
Define a type at will, without manually defining its parent class explicitly, then the parent class of this class is the Object class
class Teacher extends Person{}
toString method
-
public String toString()
-
It is meaningless for the object to return such a string of address value, so for the subclass, it is necessary to override this method of the parent class.
-
Shortcut key generation: alt + insert -> click toString method
equals method
public boolean equals(Object obj)
Indicates whether some other object is "equal to this object"
Shortcut key generation: alt + insert -> click equals() and hashCode() methods.