Anonymous objects
* Save the code, and in many cases it is only required to call a method once.
* An anonymous object can call a method or assign a value to a member variable. It can also be passed as a parameter of a method.
public class anondemo{public static void Main (string[] args) {System.out.println (New Student (). name); System.out.println (New Student (). Sex); System.out.println (New Student (). age);}} Class student{String name= "Poppy"; String sex= "female"; int age=20;}
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/08/9A/wKiom1nkj-Sw1z2sAAAVY3gdoow908.png-wh_500x0-wm_ 3-wmp_4-s_2269510850.png "title=" 8z$9z13e}thtfryjmu4ty88.png "alt=" wkiom1nkj-sw1z2saaavy3gdoow908.png-wh_50 "/ >
Packaging
* Concept: Hide the properties and details of an object and provide access only to public methods.
* Private
* When the member variable of a class is decorated with the private modifier, you cannot directly access the member variable.
* When using private decorated member variables or member methods, you can only use them in this class
* Private to the main method of modification, but also can have a role.
public class privatedemo{public static void Main (string[] args) {Student student=new Student (); Student.name= "Yi Mark XI";//if external access to private will appear name can be accessed in student private Student.subject ();}} Class student{private String name; Private String sex; private int age; void subject () {name= "Shangui";//private there is no restriction in this class System.out.println (name+ "Selected lesson is Java"); }}
Student.name (Error)
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M00/08/BC/wKiom1nmmhCxOQncAAAZMBzo6f8347.png-wh_500x0-wm_ 3-wmp_4-s_1636494779.png "title="}ozh0]}jfw7gw%r{y9@6k]b.png "alt=" wkiom1nmmhcxoqncaaazmbzo6f8347.png-wh_50 "/ >
Name= "Shangui ballad";
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/A7/6B/wKioL1nmmNWzIZ3hAAATl282WgQ529.png-wh_500x0-wm_ 3-wmp_4-s_2335955100.png "title=" Lggcuc5vrd (bhs36n~vfmqh.png "alt=" wkiol1nmmnwziz3haaatl282wgq529.png-wh_50 "/ >
* JavaBean:
All of its member variables are decorated with private and provide an accessible setting value and a Set,get method to get the value. Different scenarios may be named differently, and the database is called the Pojo class in the application.
Public class javabeandemo{ public static void main (String[] args) { student student=new student (); student.setname ("Shangui ballad"), student.setsex ("male"); student.setage, student.subject (); }}class Student{ private String name; private String sex; private int age; Void setname (String nameparm) { name=nameparm; } //javabean class string getname () { return name; } void setsex (String sexparm) { &Nbsp;sex=sexparm; } string getsex () { return sex; } void setage (int ageparm) { age=ageparm; } int getage () { return age; } void subject () { system.out.println (name+ ":" +sex+ "," +age+ "years old"); system.out.println ("Selected class is Java"); }}
650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/A7/6C/wKioL1nmnwXQRDS_AAAXXwTg2Bk619.png-wh_500x0-wm_ 3-wmp_4-s_2300127171.png "title=" eg_j~%v~slv[$21 (hr67y}f.png "alt=" wkiol1nmnwxqrds_aaaxxwtg2bk619.png-wh_50 "/ >
Precedence principle: For a member method of a class, if the variable name of the passed formal parameter is the same as the member variable name of the class, the JVM takes precedence as the method's formal parameter when processing.
this keyword
Represents a reference to the current object (which can be understood as who is currently called, and which is represented by this in the method).
For a member variable can be distinguished out of the scene, this can be added or not added, the actual effect is the same. But for the implementation of Javabean/pojo, we recommend adding the Setxxx,getxxx method.
Cases:
void SetName (String name) {
This.name=name;
}
String GetName () {
return this.name;
}
* How to initialize class member variables
* Assign values while defining member variables.
* Provides methods that are accessible to the outside world and are assigned in the method.
* The assignment is made by constructing the method.
Public class variabledemo{ public static void main (String[] args) { student student=new student (); student.write (); student student1=new student ("Shangui ballad");//Construction method assignment student1.write (); student student2=new student (); student2.setname ("Day net Sand");//Assign value in method student2.write ();}} Class student{ string name= "Yi Mark XI";//Assign a value while defining a member variable student () { } student (string name) { This.name=name; } void setname (String name) { this.name=name; } string getname () { return this.name; } void write () { system.out.println ("name:" +name); } }
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/A7/7A/wKioL1nnHYrALMexAAAYmr0Lptk218.png-wh_500x0-wm_ 3-wmp_4-s_610180405.png "title=" Man lan 1.png "alt=" Wkiol1nnhyralmexaaaymr0lptk218.png-wh_50 "/>
Static
* Loaded with class loading (static method area to save static member variables)
* Precedence over object creation
* Shared by all objects of the class (static member variables are stored in the class's static method area, so each object sees the same copy.)
* Can be accessed by class name or by object name (typically accessed through the class name).
* Static members, commonly referred to as class-related members.
The static modifier can either decorate member variables or decorate member methods. and is modified, it can be accessed directly through the class name.
* This variable cannot be used in a static method
* Static member variables and static methods can only be accessed in static methods; Non-static methods can access both static members and methods as well as non-static members and methods.
public class staticdemo{public static void Main (string[] args) {person person=new person (); Person.setname ("Shangui ballad"); Person.skill ();//Access}}class person{static String name through the class name; static String sex; void SetName (String nameparm) {name=nameparm; } String GetName () {return name; The skill of the static void skill () {System.out.println (name+ ") is:" Absolute air inverse null "");}}
non-static, although it is possible to access static methods, it is not recommended.
* Main method.
Modifier returns the type method name (parameter list) {
}
public static void Main (string[] args) {
}
* Public: One of the largest access rights, enabling the JVM to successfully invoke
* Static: Class-related, independent of the object. A.main ([]);
public class a{
Main () {
System. ("");
}
}
Java Learning Lesson 06