March 26, 2018 Java

Source: Internet
Author: User
Tags modifier modifiers

First, what is object-oriented?

In the real world, a thing that is everywhere is object, object is the entity that things exist , such as human, desk, computer, tall buildings and so on. The way human beings solve problems is always to simplify the complex things, and then think about what these objects are made of. Objects are usually divided into two parts, the dynamic and the static ones. Static part, as the name implies, is an active part, this part is called " attribute ", any object will have its own attributes, such as a person, it includes height, weight, gender, age and other attributes. However, those who have these attributes will perform what actions are also a part of the discussion, the person can cry, smile, talk, walk, These are the person's behavior (dynamic part) is the method, the human through the study of the properties of the object and observe the behavior of the object to understand the object.

Ii. Class ( encapsulation, inheritance, polymorphism )

A class is a vector that encapsulates the properties and behavior of an object, whereas a class of entities that have the same properties and behavior are called classes.

1. Encapsulation

Encapsulation is the core idea of object-oriented programming, which encapsulates the properties and behaviors of objects, while the carriers that encapsulate the properties and behaviors of objects are classes, which often hide their implementation details from the customer, which is the idea of encapsulation.

public class Xiyourenwu {    private String name;    Private String weapon;    Public String GetName () {        return name;    }    public void SetName (String name) {        this.name = name;    }    Public String Getweapon () {        return weapon;    }    public void Setweapon (String weapon) {        this.weapon = weapon;    }    Xiyourenwu (String n,string w) {        name=n;        weapon=w;}    }

2. Inheritance

classes also have a relationship with classes, such as a department store class that is associated with a salesperson class, which is called an association between classes. Correlation is a general two-dollar relationship between two classes, such as a department store class and a salesperson class that is an association, and then the student class and the teacher class are also an association. There are many kinds of relationships between two classes, and inheritance is one of the associations.

3. polymorphic

Polymorphism allows programs to be written in a uniform style to handle a wide variety of existing classes and related classes. The uniform style can be implemented by the parent class, and the object of the subclass can be instantiated according to the uniform style of the parent class. As the whole event is only dependent on the method of the parent class, it is possible to maintain and adjust the parent class's methods later, which reduces the difficulty of maintenance and saves time. When it comes to polymorphism, you have to mention abstract classes and interfaces, because polymorphic implementations do not rely on specific classes, but rather on abstract classes and interfaces.

Iii. related knowledge of class

member Variable: The property of an object in Java is called a member variable, or it can be called a property. in order to understand the member variables, first define a book class, the member variables corresponding to the properties of the class object, set 3 member variable ID, name, category in the Book class, respectively, corresponding to the books number, book name and book category of the 3 book properties.

public class Book {    //define attribute    //member variable    int id;           Book number    String name;      Figure title    String category;  Book Type}

member Method: Use the member method in the Java language to correspond to the behavior of the class object. Take the book class as an example, it contains the GetName () and SetName () two methods, the two member methods are the methods to get the name of the books and set the book name. The syntax format for defining member methods is as follows:

The permission modifier returns a value type method name (argument type parameter name) {

...//Method body

return value;

}

public void Printname () {            //No return value for member method        SYSTEM.OUT.PRINTLN (name);    }
Public String printcategory () { //member method with return value returns category; }

Local variables: Local variables are created when the method is executed and destroyed at the end of the method execution. Local variables must be assigned or initialized when they are used, or compile errors will occur.

The valid range of a local variable can be called the scope of the variable, and the valid range of the local variable starts at the declaration of the variable and ends at the end of the variable.

public void Min (double a,double b,double c) {        double sum;       Sun is a local variable        sum=a+b+c;            SYSTEM.OUT.PRINTLN (sum);    

Permission modifiers: The permission modifiers in Java primarily include private, public, and protected, which control access to member variables and member methods of classes and classes.

If a member variable or member method of a class is decorated as private, the member variable can only be used in this class, is not visible in subclasses, and is not visible to classes of other packages.

If you set the access permissions for the class's member variables and member methods to public, you can use them in subclasses and classes in other packages in addition to the data that you can use in this class.

If a class uses the protected modifier, only subclasses or other classes of that class within the package can access member variables and member methods in this class.

If the access rights of a class are set to private, the class hides all the data in it, so that the user is not directly accessible to it. You can set this class to public access if you need to make the data in your class or classes in other packages available.

public class Book {    //define attribute    //member variable    private int id;           Book Number    private String name;      Figure title    private String category;  Book type public    void Printname () {            //No return value for member method        SYSTEM.OUT.PRINTLN (name);    }    Public String printcategory () {      //member method with return value returns        category;    }}

Construction method : The constructor method is a method with the same name as the class, and the creation of the object is done through the construction method. Whenever a class instantiates an object, the class automatically calls the constructor method.

The construction method is characterized by the following: The constructor does not return a value.

The name of the constructed method is the same as the name of this class.

public class Jidongche {    private String Chepai;    private int chesu;    Private double Zaizhong;
Jidongche () { //Default existing construction method }
}
public class Jidongche {    private String Chepai;    private int chesu;    Private double Zaizhong;
Jidongche () { //assigns a value directly in the constructor method, allowing the object's property value to be initialized when the object is instantiated chepai= "XX1234"; chesu=100; zaizhong=100;} }
public class Jidongche {    private String Chepai;    private int chesu;    Private double Zaizhong;    Jidongche (String chepai,int chesu,double zaizhong) {  //with formal parameters, parameter        Setchepai (CHEPAI) must be passed when instantiating an object;        Setchesu (Chesu);        Setzaizhong (Zaizhong);    }

March 26, 2018 Java

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.