class and object

In development, we must remember that there are classes first, and then objects.

Class: Represents a common product and is a comprehensive feature.

For example, a teacher is a class composed of teachers of various disciplines.


Object: A product of a personality, a characteristic of each. is a real existence.

For example, Xiao Ming is a real person.


A class consists of properties and methods.

Attribute: Equivalent to a characteristic.

Method: It is equivalent to a person's behavior: such as: eating, sleeping, talking, etc.


Definition Format of Classes and Objects

A class can be defined in JAVA using the following statement:

class class name{

    property name;

    return value type method name(){}


Object definition:

In order for a class to really operate, it must rely on objects. The definition format of objects is as follows:

The first:

className objectName = new className();

The second:

declare object: class name object name = null;

instantiated object: object name = new class name();


Objects can be generated according to the above objects.

Call the property in the class:

object.property;

Call the method in the class:

object.method();

Example:

class Person{ //Define a class

    //Define two properties in the class

    String name;

    int age;

    public void tell(){ //Define a return value type and method name

            System.out.println(name+age);

}

public class TestDemo{

    public static void main(String[] args){

        Person per =new Person(); //Define an object and declare and instantiate it

        per.name = "Wang Er"; //object.attribute=content

        per.age=20; //object.property=content

        per.tell(); //call the method in the class through the object

    }

}

}


If no property content is set, the default value appears.



Guess you like

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