Java constructor

Constructor

  • The name of the Java constructor must be the same as the class name, and there is no return value, which can be public or private
  • Java has constructors, but no destructors. Constructors are the process of creating objects, and destructors are the process of clearing objects.
  • The new object is equivalent to opening up memory space, and the destructor is equivalent to clearing the opened memory space
  • Every variable has a life cycle, after the life cycle it dies, it can only be stored in the pair closest to {}it
  • The variable is created, the variable occupies the memory location, when the variable dies, the system will reclaim the memory, the variable cannot be resurrected after death, Amitabha
  • Variables are created through constructors and recycled through destructors, but Java does not have destructors, why? , because Java provides an automatic memory recovery mechanism, when the variable exits the life cycle, the JVM will automatically reclaim the memory of the allocated object, so no destructor is required to release the memory
  • The efficiency of object recovery depends on 垃圾回收器 GCthe recovery algorithm, and its recovery algorithm is related to the performance, which is the focus of JVM research.

Every Java class must have a constructor , otherwise the object cannot be created.
If we do not actively define a constructor, the Java compiler will automatically generate an empty no-argument constructor for the class. There can be multiple constructors, as long as Their formal parameter lists are not the same. When a new object is used, the corresponding constructor is automatically matched according to the actual parameters. If the actual parameter parameters do not match, an error will be reported.


Information hiding and this keyword
Object-oriented has a principle: information hiding

  • Class member properties, private
  • Class methods are shared, public
  • The operation of class members from the outside world can only be done through the get/set method. The shortcut key in the idea isAlt +Insert

Information Hiding Principle

  • Protect attribute information
  • Disclosure of Conduct Information

Similar to the function black box, you just use it. You don’t need to know what parameters are in the function or what happened. You only need to know the input parameters, and it will give you the output, which greatly improves the security.

  • this is responsible for pointing to members of this class
  • this can be used in place of constructors in this class

inherit

The most prominent feature of object-oriented is inheritance, encapsulation, polymorphism
, inheritance of variable types
insert image description here

output hello

  • Compared with process-oriented, there is no inheritance in process-oriented, and there will be repeated definitions.
  • Things are clustered by like, all things are objects, and objects can also be divided into several categories
  • Attributes and methods within a class have certain commonalities. Extracting and abstracting these commonalities is called parent class/superclass/base class. Other classes are subclasses or derived classes of this class.
public class demo12 {
    
    
/*    public class Man{
        int height;
        int weight;
        public void eat(){};
        public void plough(){};//耕田
    }
    public class Woman{
        int height;
        int weight;
        public void eat(){};
        public void weave(){};//织布
    }*/
    //提取整合  共同点  作为父类
    public class Hunam{
    
    
        int height;
        int weight;
        public void eat(){
    
    };
    }
    //继承,只需要单独加一个耕田方法即可
    public class Man extends Hunam{
    
    
        public void plough(){
    
    };//耕田
    }
    //继承,女人,继承了人的身高体重等属性,还有吃的方法,单独不一样的是织布
    public class Woman extends Hunam{
    
    
        public void weave(){
    
    };//织布
    }
}

Extract commonalities from multiple classes (objects), form a parent class, and then extract, relative to the parent class, it has always been like this, other classes inherit the parent class and become subclasses, which also have these characteristics

  • The subclass inherits all the properties and methods of the parent class (but cannot directly access private members)
  • Information hiding principle: The methods are public, so the subclass will inherit all the methods of the parent class and can be used directly
  • In the case of properties and methods with the same name, this class has higher priority than the parent class
  • Single-root inheritance principle: each class can only inherit one class, C++ supports one class to inherit multiple classes
  • If you don't write extendsJava default inheritance objectclass
  • All classes in Java java.lang.objectstart built into a relational inheritance tree

  • Every Java class must have a constructor
  • If there is no explicit defined constructor, the compiler will automatically generate an empty parameterless constructor for the class. If there is a displayed parameterized constructor, the compiler will not add it for you.
  • The first sentence of the constructor of each subclass calls the no-argument constructor of the parent class by default super(), unless the first sentence of the subclass constructor is not super, the superstatement must be on the first line, and the superstatement can only appear once

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324320355&siteId=291194637