Fifth day of Java learning

Source: Internet
Author: User

Object-oriented three kinds of features: inheritance, encapsulation, polymorphism.

Later in the development process is to find objects to use, no object to create an object. (Find objects--Create objects--use objects---maintain objects).

Class and object relationships:

Real-life objects: Zhang Shan, John Doe

To describe is to extract the common content in the object, to abstract the specific.

Description: For example, these features include: name, age, gender, and the ability to learn java.

Class is the description of real life.

Object: This is the kind of thing, the real existence of the individual.

In Java, classes are defined by class, with objects that correspond to the entities that Java creates in heap memory with new.

member variables and local variables:

A member variable acts on the entire class, and the local variable acts in the function or in the statement.

The member variable is in heap memory because the object exists in memory.

Local variables are in the stack memory.

=========================================================================================== Anonymous object Use one of the methods:

1, when the method of the object only need to call once can use Anonymous (no Name) object to complete the simplification of this write.

If you make more than one member call to an object, you must give the object a name.

How to use anonymous objects two:

An anonymous object can be passed as an actual parameter.



Packaging:

Encapsulation: Refers to the properties of hidden objects and implementation details, only provide public access to the external.

Benefits:

Isolate changes, ease of use, improve reusability, and improve security

Principles of Encapsulation:

Hides content that does not need to be provided externally, hides properties, and provides public access to it.

Encapsulating private: The privacy modifier: used to decorate members in a class (member variables, member functions)

Private is only valid in this class.

Note: Private is only a representation of the package.

==========================================================================================

constructor function:

Characteristics:

The function name is the same as the class name; no return value types are defined;

Class test{
Test () {
SYSTEM.OUT.PRINTLN ("person run");
}

}
Class test1{
public static void Main (string[] args) {
Test t=new test ();

}
}


# #对象一建立就会调用与之对应的构造函数. Constructor function: Can be used to initialize an object.

Small details of the constructor:

When a constructor is not defined in a class, the system defaults to a constructor that adds an empty argument to the class;

Constructors differ from general functions in their notation.

are also different on the run.

Constructors run to object initialization when the object is established.

Instead, the general method is performed by the object invocation, adding functionality to the object.

One object establishes a constructor value to run once

The generic method can be called multiple times by the object


When do you need to define a constructor function?

When a document is analyzing something, it has some characteristics or behavior, then it is defined in the constructor

===========================================================================================

Overload:

Class test{
private String name;
private int age;
Test () {
System.out.println ("A:name=" +name+ ", age=" +age);
}
Test (String N) {
Name=n;
System.out.println ("B:name=" +name+ ", age=" +age);
}

public void SetName (string n) {

Name=n;

}

Public String GetName () {

Return name}


Test (String N,int a) {
Name=n;
Age=a;
System.out.println ("C:name=" +name+ ", age=" +age);
}}
Class test1{
public static void Main (string[] args) {
Test t=new test ();
Test t1=new Test ("LISI");

T1.setname ("Libushi")

Test t2=new Test ("Lisi", 10);

}
}


Output Result:

A:name=null,,age=0
B:name=lisi,,age=0
c:name=lisi,,age=10

Constructing blocks of code: The function is to initialize the object, and the object will run and take precedence over the constructor.

Differences from constructors:

Constructing a code block is a unified initialization of all objects.

The constructor is initialized to the corresponding object.

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/89/9E/wKiom1gYUESRvalcAADWAdx_bfo364.png-wh_500x0-wm_3 -wmp_4-s_2870647462.png "title=" qq picture 20161101162124.png "alt=" Wkiom1gyuesrvalcaadwadx_bfo364.png-wh_50 "/>

The initialization of the commonality of different objects is defined in the Construction Code block code.

===========================================================================================

This keyword uses:

Class test{
private String name;
private int age;
Test () {
System.out.println ("A:name=" +name+ ", age=" +age);
}
Test (String name) {
This.name=name; /*this appears to be used to differentiate between local variables and member variables with the same name */
System.out.println ("B:name=" +name+ ", age=" +age);

}
Test (String N,int a) {
Name=n;
Age=a;
System.out.println ("C:name=" +name+ ", age=" +age);
}}
Class test1{
public static void Main (string[] args) {
Test t=new test ();
Test t1=new Test ("LISI");
Test t2=new Test ("Lisi", 10);

}
}
This: which represents the object of this class?
This represents the reference to the object to which the object is located: Simply say which object is calling the function where this is, and this represents which object.
This is used to call each other between constructors.




















Java Learning Day Fifth

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.