Home > Back-end >  Java review in the fourth chapter
Java review in the fourth chapter

Time:04-20

scope of each modifier
Private: only in this class, use the
The default (not write) : can be used in this class, and can be used in this package for other classes
Protected: this class, this package, different package subclass used in the
Public: any package, any class can use
Note:
1. Private use: define a member variable need private
2. Public use to define member method, construction method, define the class
code block
 
/* *
Block of code: {} wrapped code is a code block
Local code block: write code in the method of
Tectonic blocks of code: write in the members of the position (class, method) the block of code
Static code block: write method (class) to members of the position is decorated the static block of code that
*/
Public class DemoStudent {
Public static void main (String [] args) {
/* *
Local code block: modified variable scope improve efficiency
Variable scope: within the scope of the variable's {} effective, out of the scope, it will be garbage collected
*/
{
Int a=10;
System.out.println(a);
}
//System. Out. Println (a);//always resolve a 'a'

System. The out. Println (s1) getName () + "\ t" + s1. GetAge () + "\ t" + s1. GetSex ());
Student s2=new Student ();
System. The out. Println (s2) getName () + "\ t" + s2. GetAge ());
Student s3=new Student (" bill ", 20);
}
}

Public class Student {
Private String name;
private int age;
Private static String sex;
/* *
The characteristics of tectonic block: priority structure, every object creation, will perform a
Function: 1. Can be a member variable assignment
2. You can put in each constructor are extracted from common code, written in the book of tectonic block of code
Requirements: whether to use empty parameters | full constructor to create objects, all print 10 times HelloWord
*/
{
This. The name="* *";
Enclosing the age="18";
System. Ouut. Println (" tectonic block ");
//construction method in common blocks of code
/* * the for (int I=0; I & lt; 10; I++) {
System. The out. Println (" HelloWord ");
}
*/
}
/* *
Static code block: write in the members of the position (class method) is decorated the static block of code that
Features:
The static modifier members belong to the class, do not belong to an object, no matter how many times to create objects that will only perform a
Static priority to load into memory, prior to the structural block and structural method performs
Function:
1. You can give it an initialize static member variables values
2. In the process of project started to do some initialization designed to perform a database (only)
*/
The static {
Sex="male";
System. The out. Println (" static block ");
}
Public Student () {
/* * the for (int I=0; I & lt; 10; I++) {
System. The out. Println (" HelloWord ");
}
*/
}

Public Student (String name, int age) {
this.name=name;
this.age=age;
System. Out.println (" Person full parameter constructor!" );

}
Public String getName () {
return name;
}

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

Public int getAge () {
return age;
}

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

Public static String getSex () {
Return sex;
}

Public static void setSex (String sex) {
Student. Sex=sex;
}
}


Object class
Object class is the class hierarchy of the root class (the top class)
Each class Object class is used as a super class, any class directly or indirectly inherits the Object class
The toString method of Object class
 
Public class Person extends Object {
private String name;
private int age;
Public Person () {
}

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

//Alt + insert override toString method of Object class
@ Override
Public String toString () {
Return "Person {+"
"Name='" + name +' \ '+'
"Age=" + age +
'} ';
}

Public String getName () {
return name;
}

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

Public int getAge () {
return age;
}

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

}

/* *
Common members of the Object class methods
String toString () returns a String representation of this object,
Object class toString method source:
Public String toString () {
Return getClass (). The getName () + "@" + Integer. ToHexString (hashCode ());
}
GetClass (). The getName () : the full name of the class of a class of technology acquisition using reflection. (package name + class name) com itheima. Demo04Object. Person
"@" : the string the same output
HashCode () : in the Object class method, can obtain the hash value of the Object, the hash value is the system is given a random decimal integer 100000
Integer. ToHexString (hashCode ()) : can convert decimal Integer to hex 0-9 a - f
Together: om itheima. Demo04Object. Person @ AS12sdA address values (object)
*/
Public class Demo01ToString {
Public static void main (String [] args) {
/* *
The Person class default inherited Object class, so can use the toString method in the Object class
*/
Person p=new Person (" Dillon hot bar ", 18);
String s=p.t oString ();
System. The out. Println (s);//com. LLZ. Demo04Object. Person @ 4554617 c | 1 | Person [name=Dillon and hot: the age 18]=

//print the name of the object, is called the object's toString method
System. The out. Println (p);//p=p.t oString (com). The LLZ. Demo04Object. Person @ 4554617 c | 1 | Person [name=Dillon and hot: the age 18]=

/* *
Note:
Later to see if a class rewrite toStirng method, print the object name to see whether to address values
Is the address values: no rewrite the toString method
Not address values: rewrite the toString method
*/
String STR=new String (" aaaa ");
System.out.println(str);//aaaa String class overrides the toString method

Random r=new Random();
System. The out. Println (r);//Java. Util. Random @ 74 a14482 the Random class without rewriting the toString method

Scanner sc=new Scanner (System. In);
System. The out. Println (sc);//Java. Util. Scanner [delimiters=\ p {javaWhitespace} +] [position=0] [ma Scanner class overrides the toString method

ArrayList List=new ArrayList<> (a);
list.add(1);
list.add(2);
System.out.println(list);//[1, 2] the ArrayList class rewrite the toString method
}
}

Object class of the equals method
 
Public class Student {
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related