Java: [object-oriented: class definition, static variables, member variables, constructors, encapsulation and private, this], java Constructor

Source: Internet
Author: User

Java: [object-oriented: class definition, static variables, member variables, constructors, encapsulation and private, this], java Constructor

Content:
  • What is object facing?
  • Class Definition and Object Instantiation
  • Member variables
    • Valid range of member variables
    • Assignment of member variables
  • Static variables, methods, instance variables, and methods
  • Constructor
  • Encapsulation and private
  • This keyword

 

Note: functions and methods are the same thing. [Since some books are not the same in my previous studies, I am used to using different statements in different situations when I read many books]

 

Start Time:

What is a face object:

 

  • Object-oriented emphasizes that the behavior is in the object, instead of directly executing the behavior, but through the object execution behavior
  • A class is the abstraction of an object, and an object is a specific instance of a class. [It can be said that a person is a class, and you and I are a specific instance of different identities. The human class abstracts the attributes we humans share, and we humans are specific examples of the attributes of humans .]
  • Basic object-oriented features: encapsulation, inheritance, and Polymorphism
    • Encapsulation: for example, if an object encapsulates its attributes and methods, the attributes and methods it executes are all of this object, rather than other objects.
    • Inheritance: subclass can inherit the attributes and methods of the parent class. For example, "human" inherits the attributes and methods of "Breastfeeding" and adds the unique attributes and methods of mankind.
    • Polymorphism: A polymorphism means that the same operation has different results when executed by different objects (for example, the dog's say is "Wang", and the cat's say is ""); another polymorphism is to execute different Methods Based on Object input. [These two are essentially the same]

 

Class Definition and Object Instantiation:

 

Class Definition:
  • The class definition includes two parts: class declaration and class body.
  • Classes are the basic elements of java programs.
  • Format of the definition class:
    • The modifier can be public or empty. public indicates that the class can be accessed even if different packages are used. Empty indicates that the class can only be accessed in the same package.
    • The content of the class body includes the definition of the life and method of the variable. [So a separate definition is incorrect. It can only be declared and defined at the same time]

 

Object Instantiation
  • Class instantiation is to create an object:
    • Create an object: class name variable name = new Class Name () [Class Name () can be passed in some parameters to initialize the object, the method with the same name as the class (constructor method, as described below) define what parameters can be passed in]
      • Anonymous object: Simply put, there is no instantiation of the variable name. The new class name () is used directly for the operation. Because no variable name is used to specifically mark a piece of memory space, therefore, anonymous objects are generally used only once, for example, new Dog (). hello ();
    • Call method or variable: objects can be directly called for instance methods or variables. For static methods or variables, you can use objects or classes to call them.

 

 

Class Dog {String name; int foot = 4; Dog () {// this is a constructor this. name = "wangcai";} void hello () {System. out. println ("hello, this is a dog");} static void static_hello () {System. out. println ("hello, this is a dog too") ;}} public class Demo {public static void main (String args []) {Dog d = new Dog (); System. out. println (d. foot); // 4 d. hello (); // hello, this is a dog d. static_hello (); // hello, this is a dog too Dog. static_hello (); // hello, this is a dog too }}

        Member variables:

         

        Valid range of member variables
        • The member variables are valid throughout the class and are irrelevant to the position of the variable declaration. The local variables in the method take effect from the declared position and are only valid in the method body.
        • If you want to use a member variable with the same name as a member variable, you can use "this. variable name"
        • Member variables have default values, while local variables have no default values.

         

        Assignment of member variables:
        • You can use constructors to initialize some member variables.
        • If the member variables are not private, you can directly obtain the variable name to assign values, such as dog. age = 18.

        Static variables, methods, instance variables, and methods:

         

        Based on whether static modification of variables and methods can be divided into instance variables, instance methods, static variables (class variables), static methods (class methods)

         

        • Static variable, static method:
          • Features of static modified members:

            • Loading with classes takes precedence over object existence, and static member memory is located in the method area.

            • Shared by all objects (so it can be called class variables or class methods]

            • Can be called directly by Class Name

            • Static methods can only access static members.

            • This and super keywords cannot be written in static methods.

         
        • Instance variables and methods:
          • Instance variables and instance methods are object variables and Methods. Object operation variables or methods only operate on their own variables or methods, without affecting other objects.

         

        The differences between the instance variable \ method and the static variable \ method are as follows: for example, if there is a common attribute "dog type name", this attribute should be available to all the puppies, in addition, it is shared by dongles. If a human wants to change the type name of dongles one day, it should be changed for all dongles (static ); each dongle has its own host, which is determined by the dog itself. Therefore, this is a special attribute. Even if the dog changes its host, it will not affect other dogs. (Instance)

           

           

           

          Constructor:
          • A constructor is a special function in a class. The constructor name is the same as the class name and has no type.
          • The constructor initializes the specific attributes of the Class Object. [For example, each dog object has a different name, while the Dog class is a template without a name, and the constructor can define a name for the dog .]
          • Class can have multiple constructors that exist in the form of function overloading.
          • The constructor has no type and uses "Class Name () {}" directly as the function.
          • Class will have a non-parameter constructor by default. If other constructor is defined, the default non-parameter constructor will not exist by default.

           

           

          Class Dog {String name; Dog () {this. name = "wangcai";} Dog (String name) {this. name = name ;}} public class Init_usage {public static void main (String args []) {Dog d3 = new Dog (); Dog d4 = new Dog ("coriander "); system. out. println (d3.name); System. out. println (d4.name );}}

           

          Supplement:
          • Constructors can call each other, but do not call them recursively.
            • Calling other constructor to implement initialization has obvious code savings when multiple variables need to be initialized.

           

           

           

           

          Encapsulation and private:

           

          • One manifestation of class encapsulation is the privatization of variables and functions.
          • Encapsulation principles:
            • Hide all content that does not need to be provided externally.
            • Hide all attributes and provide public methods to access them.
          • Privatization hides the Non-Public attributes of a class. For example, if a "human" object has its own assets, only you can know how many assets you have and do not want to see them directly, but if someone else asks a question, they will still tell others (hiding their assets, but they can use the method to obtain results. This method is open, and the call is equivalent to asking others ).
          • Private can be used to privatize variables and functions. In this way, objects. variables or objects. functions cannot be called directly. Only internal methods of objects can be called.
          • After the variables or methods are privatized, the getXXX and setXXX methods are generally provided for external access to improve data access security.

           

          Class Man {private int money; String name; Man (String name, int money) {this. name = name; this. money = money;} int getmoney () {return money;} void setMoney (int money) {this. money = money ;}} public class Private_usage {public static void main (String [] args) {Man m = new Man ("lilei", 2000); System. out. println (m. name); // lilei // System. out. println (m. money); // the error message is returned because it is private and cannot be accessed. // System. out. println (m. wife); // the error message is returned because the System is private and cannot be accessed. out. println (m. getmoney (); // 2000 m. setMoney (6000); System. out. println (m. getmoney (); // 6000 }}

          This Keyword:

           

          • This indicates the current object (the object currently called by the table in the function call era)For example, you can use this. XXX in the class to call the variable or method of the object.
          • When the local variables and member variables have the same name, you can use the this keyword to distinguish them. this. XXX represents the variables of the object.
          • The member variables in the class are prefixed with this by default, but must be differentiated in case of the same name.

          • This adds the parameter list (this (parameter) to access the constructor that meets this parameter in this class.

          • The this statement used to call the constructor must be placed in the first line, because the initialization action must be executed first.

           

          class Person{    String name;    int age;    Person(String name,int age){        this.name=name;        this.age=age;    }    void hello() {        this.sysprint();//        sysprint();    }    void sysprint() {        System.out.println("hello world!");    }}public class This_usage {    public static void main(String args[]) {        Person p1=new Person("lilei",18);        p1.hello();//hello world!            }}

           

          Related Article

          Contact Us

          The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

          If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

          A Free Trial That Lets You Build Big!

          Start building with 50+ products and up to 12 months usage for Elastic Compute Service

          • Sales Support

            1 on 1 presale consultation

          • After-Sales Support

            24/7 Technical Support 6 Free Tickets per Quarter Faster Response

          • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.