1. Local and member variables
(1) The defined position is different
Local variables: inside the method;
Member variable: written directly in the class outside the method.
(2) The scope of action is different
Local variable: it can only be used in the method, and it can't be used after the method is out;
Member variables: the entire class can be common.
(3) The default values are different
Local variable: there is no default value. If you want to use it, you must assign it manually;
Member variable: if there is no assignment, there will be a default value. The rule is the same as that of array.
(4) Memory location is different
Local variable: located in stack memory;
Member variable: located in heap memory.
(5) The life cycle is different
Local variable: it is born as the method enters the stack and disappears as the method exits the stack;
Member variable: it is born as the object is created and disappears as the object is garbage collected.
public class Demo01VariableDifference { String name;//Member variable public void methodA() { int num=20;//local variable System.out.println(num); System.out.println(name); } public void methodB(){ // System.out.println(num);// Wrong writing System.out.println(name); // int age; // System.out.println(age);// Error, cannot be used without assignment. } }
2. Three characteristics of object-oriented
(1) Three characteristics of object-oriented: encapsulation, inheritance and polymorphism. Packaging is mainly introduced here, and the remaining two features will be introduced later.
Encapsulation is to hide some details from the outside world.
Embodied in Java:
1) Method is a kind of encapsulation
/*Find the maximum value The encapsulation of the method is used */ public class Demo02Method { public static void main(String[] args) { int[] array = {5,10,24,50,100}; int max = getMax(array); System.out.println("Maximum"+max); } // Use method to complete encapsulation public static int getMax(int[] array){ int max= array[0]; for (int i = 1; i < array.length; i++) { if (array[i]>max){ max=array[i]; } } return max; } }
2) The keyword private is a kind of encapsulation
/* Problem Description: when defining age, unreasonable values cannot be prevented from being set. Solution: Modify the member variable to be protected with the Private keyword. Once Private is used for decoration, it can still be accessed freely in this class However, beyond the scope of this class, it can no longer be accessed directly. To access private member variables indirectly is to define a pair of Getter/Setter methods. */ public class Person { String name; private int age; public void show(){ System.out.println("I am"+name+",Age:"+age); } //This member method is specifically used to set data to age. public void setAge(int num){ if(num<100&&num>0){ age = num; }else{ System.out.println("Unreasonable data"); } } //This member method is specifically used to get the data of age. public int getAge(){ return age; } }
public class Demo03Person { public static void main(String[] args) { Person person = new Person(); person.show(); person.name ="Ah ah"; // person.age = 10;// Direct access to private content, wrong writing. person.setAge(-20); person.show(); } }
Practice using private
/* For the boolean value in the basic type, the Getter method must be written in the form of isXxx, while the setXxx rule remains unchanged. */ public class Student { private String name; private int age; private boolean male; public void setMale(boolean b){ male = b; } public boolean isMale(){ return male; } public void setName(String str){ name = str; } public String getName(){ return name; } public void setAge(int num){ age = num; } public int getAge(){ return age; } }
public class Demo04Student { public static void main(String[] args) { Student stu = new Student(); stu.setName("Huang Da"); stu.setAge(10); stu.setMale(true); System.out.println("full name"+stu.getName()); System.out.println("Age"+stu.getAge()); System.out.println("Gender"+stu.isMale()); } }
3. The function of this keyword
public class Person { String name; // public void sayHello(String who){ // System.out.println(who + ", hello. I'm" + name "; // } public void sayHello(String name){ System.out.println(name+",Hello. I am"+this.name); System.out.println(this); } }
/* When the local variable of the method and the member variable of the class have the same name, the local variable shall be used preferentially according to the "proximity principle". If you need to access member variables in this class, you need to use the format: this.Member variable name this Keywords can solve the problem of inseparable duplicate names. Whoever calls the method is this */ public class Demo01Person { public static void main(String[] args) { Person per = new Person(); per.name = "Ants"; per.sayHello("snail"); System.out.println(per);//Address value } }
4. Construction method
Construction method is a method specially used to create an object. When we create an object through the keyword new, we are actually calling the construction method.
/* Format of construction method: Format: public Class name (parameter type parameter name){ Method body; } */ public class Student { private String name; private int age; public Student(){ System.out.println("The parameterless construction method is executed"); } public Student(String name,int age){ System.out.println("The full parameter construction method is implemented"); this.name = name; this.age = age; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setAge(int age){ this.age=age; } public int getAge(){ return age; } }
public class Demo02Student { public static void main(String[] args) { Student stu1 = new Student();//Nonparametric structure System.out.println("======================"); Student stu2 = new Student("Seagull",20);//Fully parametric structure //Change age stu2.setAge(25); System.out.println("full name"+stu2.getName()+",Age:"+stu2.getAge()); } }
Precautions: 1 The constructor name must be exactly the same as the class name, even the case. 2. The constructor should not write the return value type, not even void. 3. The constructor cannot return a specific return value. 4. If no constructor is written, the compiler will give a constructor by default, and will do nothing without parameters and method body. 5. Once at least one constructor is written, the compiler will no longer give away. 6. The construction method can also be overloaded. Overload: the method name is the same, but the parameter list is different.
5. Standard class
A standard class usually needs to have the following four components
(1) All member variables need to be decorated with the private keyword;
(2) Write a pair of Getter/Setter methods for each member variable;
(3) Write a parameterless construction method;
(4) Write a full parameter construction method.
Only standard classes are called Java beans
public class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public Student() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
public class demo01Student { public static void main(String[] args) { Student stu1 = new Student(); stu1.setName("Zhang Zifeng"); stu1.setAge(10); System.out.println("full name"+stu1.getName()+"Age:"+stu1.getAge()); System.out.println("================="); Student stu2 =new Student("Peng Yuchang",20); System.out.println("full name"+stu2.getName()+"Age:"+stu2.getAge()); stu2.setAge(25); System.out.println("full name"+stu2.getName()+"Age:"+stu2.getAge()); } }