JAVA Object-oriented classes and objects

Source: Internet
Author: User
Tags modifiers naming convention

This page was updated on July 17, 2016

Objective

Java is an object-oriented programming language, and the Java language provides basic functions such as defining classes, member variables, and methods.
A class can be considered a custom data type.
You can use classes to define variables, and all variables that use class definitions are reference variables.
They will refer to the object of the class.
Class is used to describe the common characteristics of a class of objects in an objective world.
The object is the concrete existence of the class.
Java uses the class's constructor to create objects of that class.

Java also supports the three main features of object-oriented: encapsulation/inheritance/polymorphism
Java provides private/protected/public three access control modifiers for a good encapsulation.
The extends keyword is provided to allow subclasses to inherit from the parent class.

Subclasses inherit the parent class to inherit the member variables and methods of the parent class.
If access control allows, the subclass instance can call directly the method defined in the parent class.
Inheritance is an important means of implementing class reuse.

When implemented using inheritance relationships, subclass objects can be assigned directly to the parent class variable.
This variable is polymorphic and makes programming more flexible.

The constructor is used to initialize the class instance. The constructor supports overloading.
If more than one overloaded constructor contains the same initialization code, you can place the initialization code in a normal initialization block.
is executed during the class initialization phase.
If a class in the inheritance tree needs to be initialized, all parent classes of that class will be initialized at the same time.

Defining classes

There are two important concepts in object-oriented program design: Class and Object (object is also called instance, instance).
The class is an abstraction of a group of objects that can be understood as a concept.
object is only a specific entity when it is present.
From this point of view, everyday people are actually examples of human beings, not humans.

The Java language is an object-oriented programming language, and classes and objects are the core object-oriented content.
The Java language provides syntax support for creating classes and creating objects.
The simple syntax for defining classes in the Java language is as follows:

[修饰符] class 类名{    零到多个 构造器定义...    零到多个 成员变量...    零到多个 方法...}

In the above syntax format, the modifier can be public/final/abstract or omitted from writing. (After the function of the modifier we speak again).
Where the class name must be one or more meaningful words concatenating, each word capitalized, the other letters all lowercase.
Do not use any separators between words and words.

For a definition class, you can include the three most common members: the constructor/member variable/method.
Three members can be defined with 0 or more of them. If three kinds of members are not written, it is defined an empty class, so it has no practical meaning.

The order in which the members are defined within a class can be arbitrary, and each member can be called to each other.
It should be noted, however, that members that are modified by static cannot access members that do not have static adornments.

A member variable is used to define the state data contained by the class or an instance of that class.
method is used to define the behavior characteristics or feature implementations of the class or instance of the class.
The constructor is used to construct an instance of the class. The Java language uses the new keyword to invoke the constructor, which returns an instance of the class.

A constructor is a fundamental way for a class to create objects.
If a class does not have a constructor, this class is usually unable to create an instance.
Therefore, the Java language provides a feature: If a programmer does not write a constructor for a class.
The system will provide a default constructor for the class.
Once the programmer has provided a constructor for the class, the system will no longer provide a constructor for that class.

The syntax format for defining member variables is as follows:
[修饰符] 类型 成员变量名 [默认值];

A detailed description of the syntax for defining member variables is as follows:

    • Modifier: can be omitted, or it can be public/protected/private/static/final. Of these, public/protected/private three can only be selected at most, but may be combined with subsequent static/final to modify the member variables.
    • Type: The type can be any data type allowed by the Java language, including the basic types that were previously mentioned and the reference types that are now introduced.
    • Member variable name: the member variable name should be concatenating by one or more meaningful words, with the first letter lowercase, followed by the first letter of each word capitalized. For example: HelloWorld. Do not use any separators between words and words.
    • Default: Define member variables you can specify a default value or do not write.
The syntax format for defining a method is as follows:
[修饰符] 方法返回值类型 方法名(形参列表){    //由零条到多条 可执行性语句组成的方法体}

Define method Syntax format details:

    • Modifier: can be omitted, or it can be public/protected/private/static/final/abstract. of which public/protected/private three appears at most one. Both abstract and final are one of the most. They can be combined with static to modify the method.
    • method returns a value type: The return value type can be any data type allowed by the Java language. Includes basic types and reference types; If a method return value type is declared, the method body must have a valid return statement that returns a variable or expression with the type of the variable or expression that must match the type declared here. In addition, if a method does not return a value, you must use void to declare no return value.
    • Method Name: The naming convention for method names is the same as the naming convention for member variables.
    • Formal parameter lists: parameter lists are used to define the parameters that the method can accept, and the formal parameter list is composed of 0 groups of [parameter type argument names]. Multiple sets of parameters are separated by commas. The formal parameter type and formal parameter names are separated by English spaces. Once a formal parameter list is specified when the method is defined, the corresponding parameter value must be passed in when the method is called – that is, who invokes the method and who is responsible for assigning the parameters.

A statement that has a strict order of execution between multiple executable statements in the method body is always executed first.

Static is a special keyword that can be used to modify members such as method/member variables.
The member of the static modifier indicates that it belongs to the class itself and not to a single instance of the class.
Therefore, the static modified member variables and methods are often referred to as class variables/class methods.
A method/member variable that does not use static decoration belongs to a single instance of the class, not to that class.
Therefore, member variables and methods that do not use static modifiers are often referred to as instance variable/instance methods.

Because static English literal translation is the meaning of statics.
Therefore, it is sometimes called static variables and static methods for the member variables and methods of static modification.
member variables and methods that do not use static modifiers are called non-static variables and non-static methods.
Static members cannot access non-static members directly

To define the syntax format of a constructor
[修饰符] 构造器名(形参列表){    //由零条到多条 可执行语句组成的构造器执行体}

Define the constructor syntax format in detail:

    • Modifier: can be omitted, or it can be one of public/protected/private
    • Constructor Name: constructor name must be the same as class name
    • Formal parameter list: The format of the parameter list is exactly the same as that of the definition method.

It is worth noting that the constructor can neither define the return value type nor use void to declare that the constructor has no return value.
If the constructor defines a return value type, or uses void to declare that the constructor does not return a value. Compile without error.
But Java treats this so-called constructor as a method-it is no longer a constructor.

Now, define a class with the previous content
publicclass Person{    //下面定义了两个成员变量    public String name;    publicint age;    //下面定义了一个say方法    publicvoidsay(String content)    {        System.out.println(content);    }}

The constructor is not defined in the person class code above.
As we said above, you have no definition, and Java will default to a constructor. But you can't see the naked eye. At the same time, the constructor provided by the system does not have any parameters.

After defining the class, you can then use the class.
The Java class has the following general functions:

    • Defining variables
    • Creating objects
    • Calling class methods or accessing class variables of a class

The following is a first introduction to using classes to define variables and create objects

The generation and use of objects

The fundamental way to create an object is to use a constructor.
An instance of this class can be created by invoking the constructor of a class with the new keyword.
Above we have written a person class, below write a persontest class

//使用Person类来定义一个Person类型的变量Person p;//通过 new 关键字调用 Person 类的构造器, 返回一个 Person 实例new Person();

The above code can be abbreviated to the following form:

//定义 p 变量的同时为 p 变量赋值new Person();

Once you have created the object, you are ready to use it.
Java objects generally have the following effects:

    • Accessing an instance variable of an object
    • Methods for calling Objects

If access permission is allowed, the methods and member variables defined in the class can be called through the class or instance.
The syntax for a class or instance access method or member variable is: Class. A class variable/method or instance. Instance variables/Methods
In this way, a class or instance is the keynote person that accesses a member variable or method of that class or instance.

Where Static-modified methods and member variables can be invoked either through a class or by an instance.
Instead of using static decorated methods or member variables, you can only invoke them through an instance.
The following code invokes the member variables and methods of the person through the person instance

//访问 p 的 name 实例变量, 直接为该变量赋值"萌萌";//调用 p 的 say() 方法, 声明 say() 方法时定义了一个形参//调用该方法必须为形参指定一个值p.say("呵呵哒呵呵呵呵哒");//直接输出 p 的 name 实例变量System.out.println(p.name);

The Say () method is called through the person instance in the code above, and must be assigned to the parameter of the method when the method is called. Therefore, when you call the Say () method of the person object in this line of code, you must pass in a string for the Say () method as the parameter value of the formal parameter.
This string is passed to the content parameter.

Most of the time, a class is defined to create an instance of the class repeatedly.
Multiple instances of the same class have the same characteristics.
A class is a common feature that defines multiple instances.
From a certain point of view, the characteristics of multiple instances are defined.
Therefore, the class is not a concrete existence, the instance is the concrete existence.
It can be said that you are not human this class, I am not human this class, we are just human examples of this class.

JAVA Object-oriented classes and objects

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.