Object-oriented structured programming and object-oriented programming
- In Java, we design around the object to solve the problem, and we describe the properties and behavior of the pair through the class. To describe all of them as objects.
- Classes are complementary to objects, classes are objects, and objects are instances of classes.
- Each property of an object is represented as a member variable in the class, and each behavior of the object becomes a method in the class
Class
A class is a collection of objects that have common properties. Create a new object to use the keyword new,new+ class name to instantiate a new object, for example:
public class Name(){}建立一个其他的类public class Other(){Name name=new Name();//这就表示实例化一个新的对象}
Once instantiated, it has all the properties and methods of the class.
constructor function
Each time we create a new object, we are creating it through a template, which is the constructor. In Java, the constructor is the default, except that it is not written out, and the instantiation of the object is through the constructor.
Without a write constructor, a parameterless constructor is created by default, and if it is written, the system is no longer created, and the instantiation of the object is done through your constructor function.
The difference between constructors and other methods
- The name of the constructor must be the same as the name of the class
-
The constructor does not return a value, nor can it declare a return value
public class Test{public Test(){super();}//无参的构造函数public Test(int a){super();}//有参数的构造函数 }
When you add multiple constructors, you need to apply the overloads of the methods, each of which must have a unique list of formal parameters. There is a super () method in the constructor, which means that the parent class can be called super.方法名 . In all objects, their root is object, so the default super is the object represented by the
this keyword
In multiple constructors, you can use the This keyword to invoke other constructors, which must be in the first row of the constructor, or an error will occur.
public class Test{ int a=0; public Test(){ this(b);}//a是实参,可以是任意int类型的 public Test(int a){ this.a=a}}//把接收到的a的值赋给开始定义的a
Inheritance (extends)
- In object-oriented programming, a new class is created by extending an existing class and inheriting the class's properties and behavior, which becomes the inheritance
-
In inheritance, an existing class is called a parent class. The new class is called a subclass. A parent class can be called a base class, a superclass
public class Student{ public void say(){//声明实现一个方法System.out.print("被继承了");}}//新建一个类去继承Studentpublic class Test extends Student{ public void test(){ this.say();}通过this或是super关键字来调用父类的方法 @Override//重写父类的方法 public void say(){} }
In object-oriented programming, when two or more classes are different, but with common features, we take common elements out and put them in a parent class. This uses inheritance to not only reuse existing code to avoid code duplication, but also to create a program that is easy to maintain and modify code
An "is a" relationship can be used in inheritance to determine whether the inheritance of the parent class and subclass is correct. When using inheritance, we must be able to say that the subclass is a (a) parent, and if it is, the inheritance is correct.
A subclass of Java can inherit only one parent class, but a parent class can also have a parent class
Method overrides
- The return value type, method name, and formal argument list for the subclass must be the same as in the parent class. If a method in a subclass has the same name as a method in the parent class, the method in the subclass changes the formal argument list, which is the method overload instead of overriding the
- The access modifier must be not less than the access modifier in the parent class
Final keyword
Final Method when a subclass overrides a method of a parent class, the overridden method in the parent class is essentially hidden. In a subclass, the only way to invoke a method in the parent class is to display the call. If you do not want the subclass to be overridden, you can declare the method as the final method. The final method cannot be overridden by a quilt class
Access modifiers
- Public (publicly accessible level): Members that are decorated with the public keyword are exposed externally, that is, exposing members can be accessed by any other object
- Protect (Protected access level): Decorated with the Protect keyword, protected members can be accessed by classes in the same package, and can be accessed by subclasses of the class, regardless of which package the subclass is in
- No access modifier (default access level): Members of the default access level can be accessed by other classes in the same package
- Private access level: Use the private keyword modifier. He is the lowest level in the access modifier. Private members can only be accessed by the class itself, not publicly
PS: protected and default access is similar because both grants access to other classes in the same package. The protected access type is actually less restrictive than the default access because protected Access also grants access to subclasses outside the package
本类 同包 子类 外包public √ √ √ √private √ × × ×protect √ √ √ ×默认: √ √ √ × ↓↓ 默认的子类出了包就不能访问
Packaging
Encapsulation is the technique of making member variables in a class private and providing public methods to access these member variables. If a member variable is declared private, it cannot be accessed by other classes, thereby hiding the member variables in the class. Therefore, encapsulation is also known as data hiding
Java Learning (vi)