1.1 javaBean
JavaBean: JavaBean is a public Java class, but there are at least three conditions to satisfy for editing tool recognition: there is a public default constructor (such as a parameterless constructor,)Attributes are accessed using the public get, set method, that is, set to private, and get, which corresponds to the size of the property name. For example, property name, get method is written, public String getName(){},N is capitalized. Serialization is required. This is a framework where tools must be viewed recently to reflect state across platforms <Think in Java>,It says that JavaBean was originally implemented for visual programming of Java GUI. When you drag an IDE build tool to create a GUI component (such as a multi-checkbox), the tool actually creates a Java class for you, exposes its properties, modifies it, and exposes the event listener.
EJB: In enterprise development, scalable performance and transaction, security mechanisms are required to ensure smooth development of enterprise systems, rather than developing to a scale and replacing a set of software systems. Enterprise Bean appears when there are increased protocol requirements. EJB puts forward some requirements on the basis of javabean, which is of course more complex.
POJO: A person named Josh MacKenzie thought that EJB was too complex to be used every time, so he invented POJO. POJO is a normal javabean. What is normal is corresponding to EJB. In short, the difference is that you first decide if the condition of JavaBean is met, then if you implement some requirements, it is EJB, otherwise it is POJO.
1.2
(1) All attributes are private
(2) Provide default construction methods
(3) Provide getter s and setter s
public class OOP_03 { public static void main(String[] args) { Customer c = new Customer(); c.setAge(18); c.setName("Zhang San"); System.out.println(c.getName()); System.out.println(c.getAge()); // Re-assign c to null c = null; // java.lang.NullPointerException null pointer exception // When we use the null value to call member properties, a null pointer exception is reported // System.out.println( c.getAge() ); //Object Call Static Properties c=null; // There will be no errors because i is a static property and there is no relationship between root and heap memory // When we call a static property with a reference type variable, it is converted to the corresponding class name at compile time to be called System.out.println(c.i); Customer c1 = new Customer(); // Whether null or not, it will be converted to a class name call // So static variables are properties and values common to all objects System.out.println(c1.i); System.out.println(Customer.i); } } class Customer { static int i = 2; private int age; private String name; public void setAge(int age_) { age = age_; } public void setName(String name_) { name = name_; } public int getAge() { return age; } public String getName() { return name; } }
1.3 Common anomalies
1.4 Passing Values and References
package day_02._02_OOP; public class OOP_04 { public static void main(String[] args) { int i = 10; m1(i); System.out.println(i); System.out.println("===="); A a = new A(10); m2(a); System.out.println(a.age); } public static void m2(A a) { a.age++; System.out.println(a.age); } public static void m1(int i) { i++; System.out.println(i); } } class A { int age; public A(int age_) { age = age_; } }
1.5 Variable Level Priority
package day_02._02_OOP; public class OOP_05 { // Static and member variables cannot have the same name int a = 1; // static int a = 1; static int b = 1; public static void main(String[] args) { // Variables that cannot have different names in a scope int i = 2; // int i = 1; // Local variables can have the same name as static and member variables int a = 4; int b = 5; // When a local variable has the same name as a static variable, the local variable takes precedence over the static variable in the method. System.out.println(b); // To use static variables, you need to distinguish them by class name System.out.println(OOP_05.b); } }
1.6 How to distinguish construction methods from member methods of the same name
Membership and construction methods can have the same name,Return value is required to distinguish Because of the construction method,no return value,even void None And membership methods,Has Return Value,Write without returning data void Of public class OOP_06 { public static void main(String[] args) { // OOP_06 o = new OOP_06(10); OOP_06 o1 = new OOP_06(); } public OOP_06() { System.out.println("I am the construction method"); } public void OOP_06(int i) { System.out.println("I am the construction method"); } }
On behalf of students public class Student { // Static variables are attributes and values common to all students // Member variables are attributes common to all students // School Number String id; // Full name // private privatization, accessible only by itself, is available only in the current class body private String name1; // Age int age =2; // Gender true for female and false for male boolean sex; // address String addr; // xxx // Requirement: Hello Print, my name is XXX I am xxx old this year public void println(){ System.out.println("How do you do,My name is"+name1+" I am this year"+age+"Age"); } // Requirement: Provide a way to get the value of the name property public String getName(){ return name1; } // Requirement: Provide a way to set the value of the name property public void setName(String name2){ if (name2 == null) { name1 ="obtuse"; return; } name1 = name2; } }
2.1This
this: is a member variable of the reference type that holds its own memory address in each object
this represents the object itself, which is equivalent to saying "I"
What can this do?
1 Used in member methods and construction methods, you can distinguish between local variables with the same name and member variable syntax: this.xxx = xxx;
public class This_01 { public static void main(String[] args) { This_01 t = new This_01(1); t.setAge(10); System.out.println(t.age); } int age; public This_01(int age) { this.age = age; } public void setAge(int age) { this.age = age; } } [Click and drag to move]
2 Used in a construction method
Use to overload calls to other constructors in the current class to improve code reuse syntax: this (parameter);Must be written on the first line of the construction method
public class This_02 { public static void main(String[] args) { MyDate date1 = new MyDate(); // October 0, 0, 0 date1.print(); date1.setYear(2021); date1.setMonth(10); date1.setDay(12); // October 12, 2021 date1.print(); // Requirement: When creating a time object, there must be a year, month, day (full-parameter construct) MyDate date2 = new MyDate(2008, 8, 8); // 8 August 2008 date2.print(); // Requirement: When creating a time object, you can pass in the year, month, day or not // If not passed, the default is January 1, 1970 (one full parameter, one none) MyDate date3 = new MyDate(); // January 1, 1970 date3.print(); } } class MyDate { // Member variables private int year; private int month; private int day; // Total parameter construction public MyDate(int year, int month, int day) { this.year = year; this.month = month; this.day = day; // xxxx } // Parametric construction public MyDate() { // this.year = 1970; // this.month = 1; // this.day = 1; // xxxx // Overload calls the parametric construct above for code reuse // But the statement must be on the first line of the construction method this(1970, 1, 1); } // getter/setter public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } // Requirements: Print year, month and day in specified format public void print() { System.out.println(year + "year" + month + "month" + day + "day"); } }
3 return this;Chain call is possible
Core Principle: The return value of the former must be a reference that calls the latter
In a member method, which object calls this method and which object is this
public class This_03 { public static void main(String[] args) { This_03 t = new This_03(); t.m1(); t.m2(); // call chaining // The return value of the former must be a reference that calls the latter t.m1().m2().m2().m2().m1().m2(); } public This_03 m1() { System.out.println("I am m1 Method"); return this; } public This_03 m2() { System.out.println("I am m2 Method"); return this; } }
2.2Static
Static: is a modifier that distinguishes static from member
You can modify three things: static variables, static methods, static statement blocks
Static Variables: Variables in the class body modified with static
Static methods: methods decorated with static
Static statement blocks: Code blocks decorated with static (statement blocks)
A brace is a block of sentences
Syntax: static {java code} executes during the class loading phase, immediately after loading is complete, before main, and only once
Prepare for initialization because it is performed first and only once
public class Static_01 { public static void main(String[] args) { System.out.println("main Method"); } // Multiple statement blocks, executing from top to bottom static { System.out.println("Static statement block 1"); } static { System.out.println("Static statement block 2"); } }
Instance statement block: can be seen as a member method without a name
Each time an object is created, it is executed once, many from top to bottom
Syntax: {code;}
public class Static_02 { public static void main(String[] args) { System.out.println("main Method"); Static_02 s = new Static_02(); Static_02 s1 = new Static_02(); } public Static_02(){ System.out.println("Construction method"); } static{ System.out.println("Static statement block"); } { System.out.println("Instance statement block"); } }
public class Static_03 { public static void main(String[] args) { System.out.println("main"); } // Static variables are also initialized during the class loading phase, and are initialized from top to bottom, consistent with the static statement block priority static int i = 1; static { System.out.println("Static statement block : " + i); } // static int i = 1; }