07.01 the difference between a member variable and a local variable
1. Different positions in the class
Outside the method in the member variable class
Inside a local variable method or on a method declaration
2. Different locations in memory
Member Variable heap memory
Local variable stack memory
3. Different life cycle
The member variable exists as the object exists and disappears as the object disappears
Local variables exist with the invocation of the method and disappear as the call to the method finishes
4. Different initialization values
The member variable has a default initialization value
A local variable has no default initialization value and must be defined before it can be used.
07.02 the formal parameter of the method is the invocation of the class name
Cases:
1 class Demo 2 {3 public static void Main (string[] args) 4 {5 //Create student Object 6 Student s = new Student (); 7
studentdemo sd = new Studentdemo (); 8 Sd.method (s); 9 }10}11 class StudentDemo12 { the//formal parameter is a reference type: public void Method (Student s) s.show (), }18}19 class Student20 {$ public void Show (), { System.out.println (" Students love to Learn "); }25}
07.03 Overview and application of anonymous objects
Anonymous object: Is an object without a name. is a simplified representation of an object
Two use cases for anonymous objects:
1. Object Invocation method only once
2. Passing as actual parameters
Cases:
1 class Demo 2 {3 public static void Main (string[] args) 4 {5 ///Anonymous Object Call Method 6 new Student (). Show (); 7 } 8} 9 class Student10 {One public void Show () { System.out.println ("Students love Learning"); }15}
Anonymous object call is complete garbage, garbage collector will be recycled at the appropriate time, improve the efficiency of memory use
07.04 Overview of the package
Encapsulation Overview: Refers to the properties and implementation details of hidden objects, and provides public access only externally.
Cases:
1 class Student 2 {3 String name; 4 //The age is private, only the 5 private int age can be accessed in the Student class ; 6 7 //provide external access to the method gets the value of the age of 8 public int Getage () 9 {Ten return age;11 }12 //provides methods for external access, And can be passed to the value of a to judge, satisfy the condition is assigned to the age, not satisfied is not assigned to the public void Setage (int a) (int. = a;16 }17 Public void Show () { System.out.println ("Name:" +name+ " " + "Age:" +age); }21}
07.05 Package Benefits and design principles
Package Benefits:
1. Hide implementation details and provide public access
2. Improved reusability of the code
3. Improve security
Encapsulation principle: Hide content that does not need to be provided externally. Hides the property and provides a public way to access it.
Overview and features of the 07.06 private keyword
Private keyword: is a permission modifier. Members (member variables and member methods) can be decorated, and members that are private-decorated are only accessible in this class.
Private most common applications:
1. Modify the member variable with private
2. Provide the corresponding getxxx ()/setxxx () method
Application standard case of 07.07 private
1 class Demo 2 {3 public static void Main (string[] args) 4 {5 Student s = new Student (); 6 //Assign a value to a member variable 7
s.setname ("Xiao Qiang"); 8 s.setage, 9 System.out.println (s.getname () + ":" +s.getage ()), }12}13 class Student14 { Private String name;16 //will be the age-owned, only in the student class to access the + private int age; //Provide external access method gets the value of name Public String getName () { return name;23 }24 //provide external access to the method setting the value of name "public void SetName" (String N) $ { n;28 }29 //provide external access to the method gets the value of the age of-Public int getage () return age;33 }34 //provide external access to the method set the value of age in the public void Setage (int a), {PNs = a;38 }39}
07.08 The This keyword overview and application
This: Represents the object reference of the class in which it resides
method is called by which object, this represents the object
Use this when a local variable hides a member variable
Cases:
1 class Student 2 {3 private String name; 4 //The age is private, only the 5 private int age can be accessed in the Student class ; 6 7 //provide external access to the method gets the value of name 8 public String GetName () 9 {Ten return name;11 }12 // Provides methods for external access set name value of public void SetName (String name), { this.name = name;16 }17 // methods to provide external access get the value of age getage Int () { age;21 }22 //Provide external access method set the value of age 23 Public void Setage (int.) { this.age = age;26 }27}
07.09 memory plot for this keyword
07.10 Standard mobile Phone class code and its test
1 class Demo 2 {3 public static void main (string[] args) 4 {5 phone p = new Phone (); 6 Sys Tem.out.println ("Brand:" +p.getbrand () + "Price:" +p.getprice () + "COLOR:" +p.getcolor ()); 7 P.setbrand ("Millet"); 8 P.setprice (1999); 9 P.setcolor ("White"); System.out.println ("Brand:" +p.getbrand () + "Price:" +p.getprice () + "COLOR:" +p.getcolor ()); 11 }12}13 class Phone14 {private string brand;//brand-Private int price;//price + private string color;//color 18 Public String Getbrand () {brand;22}23 public void Setbrand (string brand) 24 {25 This.brand = brand;26}27-public int getprice () {return price;31}32 public v OID setprice (int price) {this.price = price;35}36 PNs public String GetColor () 38 {39 return color;40}41 public void SetColor (String color) {This.color = color;44}45}
Operation Result:
Brand: null Price: 0 Color: null Brand: Millet Price: 1999 Color: White
07.11 construction Method Overview and format
Constructor Action Overview: Initialize the object's data
Construction method Format:
1. The method name is the same as the class name
2. No return value type, not even void
3. No specific return value
Cases:
07.12 overloading of construction methods and considerations
Construction Method Considerations:
1. If you do not provide a constructor method, the system gives the default construction method
2. If a construction method is provided, the system will no longer provide
3. The construction method can also be overloaded
07.13 classification and use of member methods
Method Specific division:
Based on the return value: 1. There is a definite return value Method 2. Return method of type void
According to the formal parameters: 1. No parameter Method 2. Parameter method
Cases:
1 class Demo 2 {3 public static void Main (string[] args) 4 {5 Student s = new Student (); 6 String str = S.getstring (); 7 System.out.println (str), 8 s.show (), 9 s.method ("Xiao Qiang"), }11}12 class Student13 {+- private string name;15 private int age;16 //No parameter method with a definite return value the public String getString () Hello "; }21 //Returns the non-parametric method of type void (" show Run ") of the public void Show (SYSTEM.OUT.PRINTLN); }26 //Return the parameter method of void type, the public void method (String name), System.out.println ("Name: "+name"); }31}
07.14 code and tests for a standard student class
The composition of the class:
1. Member variables
2. Construction method [Non-parametric construction method/with parameter construction method]
3. Member method [GetXxx ()/setxxx ()]
How to assign a value to a member variable
1. Non-parametric construction method + setxxx ()
2. Construction method with parameters
1/* 2 Student Class: 3 member Variable: Name,age 4 Construction Method: No parameter with two parameter 5 member method: GetXxx ()/setxxx () 6 show (): Outputs all member variable values for the class 7 8 Assign a value to a member variable: 9 a:setxxx () method B: Construction Method 11 12 How to output member variable values: A: by GetXXX () Get and then stitch the B: by calling Show () method 15 */16//test class Demo18 {public static void main (string[] args) 20 {21 Mode 1 assigns the member variable 22//No parameter Constructs +setxxx () Student S1 = new Student (); S1.setname ("xiaoming"); 25 S1.setage (27); 26//Output value System.out.println (S1.getname () + "---" +s1.getage ()); "----------------------------"); 29 30//Mode 2 assign value to member variable Student s2 = new Student ("Xiao Qiang", 30); 32 S2.show ()}34}35 Student 37 {38//name of the private String name;40//age group private int age;42 43//Construction Method-Public Student ()}47 {50-Student (String name,int Age) 49 THIS.name = name;51 This.age = age;52}53-Public String getName () (name;57}58) ublic void SetName (String name) $ {this.name = name;62}63 public int getage () 65 {66 return age;67}68 public void Setage (int.) {this.age = age;72}73 74 Output all member variable values System.out.println public void Show () (name+ "---" +age); 78}79}
07.15 a standard phone code and test
1//Test Class 2 class Demo 3 {4 public static void main (string[] args) 5 {6//Create object 7 Phone p = new Ph One (); 8 9//Assign a member variable of ten P.setbrand ("Millet"), one P.setprice (2299), one p.setcolor ("white"); 13 14//Get Value System.out.println (P.getbrand () + "---" +p.getprice () + "---" +p.getcolor ()); 16}17}18 19//Set Smart Phone class Phone21 {22//brand-Private String brand;24//price private int price;26//color String color;28 29//Non-parametric construction method public Phone () {}33//getxxx () and Setxxx () method String Getbrand () brand;38 {Notoginseng return}39 public void Setbrand (string brand) Is.brand = brand;42}43, public int getprice (), {price;47}48 public void SE Tprice (int price), {This.price = price;51}52, public String getColor () Eturn color;56 }57 public void SetColor (String color), {This.color = color;60} 61}
07.16 What things do you create objects for
Student s = new Student (); What does it do in memory?
1. Load Student.class file into memory
2. Open space in stack memory for s
3. Open space for student objects in heap memory
4. Default initialization of member variables for student objects
5. Display initialization of member variables for student objects
6. Assigning a member variable to a student object by constructing a method
7. The Student object initialization is complete, the object address is assigned to the S variable
07.17 when to define member variables
If this variable is used to describe the information of this class, then the variable should be defined as a member variable. The smaller the range of variables, the better, because they can be recycled in a timely manner.
07.18 Rectangular Case Exercises
Define a rectangular class, define a method for calculating perimeter and area, and then define a test class demo to test.
1/* 2 member Variable: length, Width 3 Member method: 4 for circumference: (length + width) * *; 5 Area: Length * Width 6 */7 import Java.util.Scanner; 8 class Changfangxing 9 {10//Rectangle long-Private int length;12//rectangle wide private int width;14 Lic changfangxing () 16 {17}18 19//Only available setxxx () can be public void setLength (int length) 21 {22 This.length = length;23}24 public void setwidth (int width)-{this.width = width;28 }29 30//for perimeter of public int Getzhouchang () + (length + width) * 2;34}35 36 The area of Panax notoginseng public int Getarea () () () (). {width;40 return length *}41}42 43//test class Demo45 {46 public static void Main (string[] args) 47 {48//Create keyboard Entry Object Scanner sc = new Scanner (system.in); 50 Wuyi System.out.println ("Please enter the length of the rectangle:"); int length = Sc.nextint (); System.out.println ("Please enter the width of the rectangle : "); sc.nextint int width = n (); 55 56//Create object changfangxing CFX = new changfangxing (); 58//Assign value to member variable Cfx.setlength (le Ngth), Cfx.setwidth (width), System.out.println ("Circumference is:" +cfx.getzhouchang ()); . OUT.PRINTLN ("area is:" +cfx.getarea ()); 64}65}
07.19 Employee Class Case Exercises
1/* 2 member variables: Employee number, name, age 3 Construction method: No parameter Construction Method 4 member Method: 5 getXxx ()/setxxx (); 6 Show (); 7 */8 9 class Employee 10 {11//Employee number + private string employeeid;13//name + private string name;15// Age + Private int age;17 18//Construction method public Employee () {}22//getxxx ()/setxxx () 24 Public String Getemployeeid () {employeeid;27}28, public void Setemployeeid (STR ing employeeId) {This.employeeid = employeeid;32}33 public String getName () 35 {36 Return name;37}38-public void SetName (String name)-{this.name = name;42}43 50 public int Getage () {51 return age;47}48-public void Setage This.age = age;52}53 54//method of displaying all member information in the public void Show () Employee Number: "+employeeid+" Name: "+name+" Age: "+age); 58}59}60 61//Test class DEMO63 {$ public static void main (string[] args) 65 {66//Create object. E = new Employee (), 68 69//Assign the member variable E.setemployeeid ("ID8899"), E.setname ("Wang Choi"); 72 E.setage (18); 73 74//Get Data//system.out.println (E.getemployeeid () + "---" +e.getname () + "---" +e.g EtAge ()); 76 77//Use the Show method for E.show (); 79}80}
07.20 Implement subtraction and test
1 Import Java.util.Scanner; 2 class MyMath 3 {4//Add function 5 public int Add (int a,int b) 6 {7 return a + B; 8} 9 10/ /subtraction function one public int sub (int a,int b), {a-b;14}15 16//multiplication function, public int mul ( int A,int b) * {25 return a * b;20}21 22//Division function + public int div (Int. a,int b) 24 Return a/b;26}27}28 29//test Class 34 class Demo31 {public static void main (string[] args) 33 {/ /Create Keyboard Entry Object Scanner sc = new Scanner (system.in); System.out.println ("Please enter first operand:"); 38 int num1 = Sc.nextint (); System.out.println ("Enter the second operand:"); + int num2 = Sc.nextint (); 41 42 Create the MyMath object and use mymath mm = new MyMath (), System.out.println (num1+ "+" +num2+ "=" +mm.add (n UM1,NUM2)); System.out.println (num1+ "-" +num2+ "=" +mm.sub (num1,num2)); System.out.println (num1+ "*" +num2+ "=" +mm.mul (num1,num2)); System.out.println (num1+ "/" +num2+ "=" +mm.div (num1,num2) "); 49}50}
Operation Result:
Please enter the first operand: 8 Please enter the second operand: 28+2=108-2=68*2=168/2=4
07.21 the introduction of the Static keyword
Cases:
1 class Person 2 {3 String name; 4 //The data that is decorated with static is called the shared data of the object 5 static String country = "China"; 6 public Voi D Show () 7 {8 System.out.println ("Name:" +name+ " Country:" +country); 9 }10}11 class Demo13 {n -public static void main (string[] args)- {person p1 = new Person (); 17 p1.name = "Xiaoming"; p1.show (); P2 = new Person (); p2.name = "Little Red"; p2.country = "Beauty p2.show (); p1.show (); }27}
Operation Result:
Name: Xiao Ming Country: China Name: Little Red Country: United States name: Xiao Ming Country: USA
Features of the 07.22 static keyword
1.static is a modifier used to decorate members (member variables and member functions)
2.static decorated members are shared by all objects
3.static takes precedence over the existence of an object because the static member already exists as the class is loaded.
4.static decorated members can be called by the object, or they can be called directly by the class name, in the form: class name. static members
5.static decorated data is shared data, objects are stored in unique data
07.23 Static Memory plot
Considerations for 07.24 Static
1. There is no this keyword in the static method
2. Static methods can only access static member variables and static member methods
3. The main function is static, and if you want to call a non-static member in the main function, you can create an object to invoke the
07.25 the difference between a static variable and a member variable
1. Different
Static variables belong to classes, so they are also referred to as class variables
Member variables belong to an object, so also known as instance variables (object variables)
2. Different locations in memory
Static variables stored in the static area of the method area
member variables stored in heap memory
3. Memory occurrence time is different
Static variables are loaded as the class is loaded and disappear as the class disappears
Member variables exist as the object is created and disappear as the object disappears
4. Call different
A static variable can be called by a class name, or it can be called by an object
Member variables can only be called by object name
07.26 A detailed explanation of the format of the Main method
Javase Learning Summary No. 07 Day _ Object-oriented 2