Home > Back-end >  Java to review the first chapter
Java to review the first chapter

Time:04-05

about the process-oriented and object-oriented
Process oriented: when we meet with one thing, our own solution steps of the analysis of things, according to the step by step to complete, emphasis is the process of things done,
Object-oriented: when we meet, we don't own work, find a can help me are complete object, call the function of the object to complete things, value is an object,
The transactions in the real life:
The properties, behavior (function)
We can put the real-life transactions abstract for Java classes in the (class is the basic unit of the Java)
The transaction attribute abstraction for the member variables in the class (defined in the member variables: the position of class, method)
The transaction abstract to members of the class method (defined in member position, the method of removing the static keyword)
Packaging: the private member variables, provides the get/set methods on public
Constructor: create an object is call in the class constructor
Format:
Modifier (parameters) constructor name {
An initial value for the member variable
}
Features:
1. There is no return value type, no add void is (no)
2. There is no return value
3. Must be the same and class name
Constructor of the matters needing attention:
1. The class does not define a constructor, the Jvm will add a no arguments for such default constructor
Format is similar to: public People () {}
2. In the class have defined the constructor (ginseng, no arguments), then the JVM would not give class to add a default constructor
3. The constructor can be overloaded (method is the same name in a hey, but different parameter list (number of order, data types))
 public Class Student {
private String name;
Private int the age;

Public Student () {
System. The out. Println (" this is an empty and construct ");
}

Public Student (String name, int age) {
this.name=name;
this.age=age;
System. The out. Println (" this is a full participation structure ");
}

Public String getName () {
Return the name;
}

Public void elegantly-named setName (String name) {
this.name=name;
}

Public int getAge () {
Return the age;
}

Public void setAge () {
this.age=age;
}


Package com. LLZ. Demo01Object;
/*
The test class:
Contains the main method of class called test class
*/
Public class Demo01Student {
Public static void main (String [] args) {
//the use of Student class no-arg constructor to create Student object
Student s1=new Student();
//using the set methods, give a member variable assignment
S1. Elegantly-named setName (" Dillon hot bar ");
S1. SetAge (18);
//using the get method, get member variable values
System. The out. Println (s1) getName () + "\ t" + s1. GetAge ());
System. The out. Println (s1);//com. LLZ. Demo01Object. Student @ 4554617 c

//use full parameters of the Student class constructor to create Student object
Student s2=new Student (" GuLiNa firm ", 20);
//using the set methods, modify member variable values
S2. SetAge (18);
//using the get method, get member variable values
System. The out. Println (s2) getName () + "\ t" + s2. GetAge ()); (note: "\ t" is the completion of the current string length to 8 integer times, at least one up to 8)
}
}
}


about this keyword
On behalf of our class this key word: object reference , which object to invoke the method, this method is to which object
This effect: when a local variable and member variable name repetition, use this can distinguish between a local variable and member variables
This. The variable name -- -- & gt; A member variable
 
Public Class Person {
private String name;

Public void show () {
String name="liu";
System. The out. Println (" name: "+ name);//liu variable principle nearby
System. The out. Println (" this name: "+ enclosing name);//this variable name next to Lao wang on behalf of the member variable
}

Public Person () {
}

Public Person (String name) {
this.name=name;
}

Public String getName () {
Return the name;
}

Public void elegantly-named setName (String name) {
this.name=name;
}
}

/* *
* test class
*/
Public class Demo01This {
Public static void main (String [] args) {
//create the Person class object
Person p=new Person (" prosperous wealth ");
P. how ();
}
}


object memory map




1: define a Student reference type variables called s1
2: create new or create a new object specific parameter value depends on the constructor or the set value after the new
3: initialize the specific value passed to the corresponding attribute












  • Related