1. encapsulation
Encapsulation Overview: refers to the hidden Object's properties and implementation details, only provide public access to External.
Benefits:
Hide implementation details and provide public access
Improved reusability of the code
Improved security
Encapsulation Principle:
Hide content that you don't need to provide externally.
Hides the property and provides a public way to access it.
2.private keywords
The Private keyword is a permission modifier that modifies member variables and member methods, and is used only in this class by private-decorated member variables or member METHODS.
private most common Applications: 1. Modify the member variable with private 2. provides a corresponding setter and getter Method.
/** * java three major features are package */class Student{ private int age; private string name; public void setage (int Age) { if (age <0 | | age >120) { return; } this.age = age; } public void setname (String name) { this.name = name; } public int getage () { Return age; } public string getname () { return name; } @Override public string tostring () { return "student{" + "age=" + age + ", name= ' + name + ' + '} '; }}public class fengzhuangdemo { public static void main (string[] args) { student stu = new student (); stu.setage, stu.setname ("hehe"); system.out.println (stu.tostring ()); }}
3.this keywords
This represents the object reference of the class in which it Resides.
/** * Mobile */class phone{ private string brand;//brand private int price;//price private string color;//color public void setbrand (string brand) { this.brand = brand; } public Void setprice (int price) { this.price = price; } public void setcolor (String color) { this.color = color; } public string getbrand () { return brand; } public int getprice () { return price; } public String getcolor () { return color; } @Override public string tostring () { return "phone{" + "brand=" + brand + "\" + ", price= " + price + ", color= '" + color + " + '} '; }}public class phonetest { public static void main (string[] args) { phone p = new phone (); p.setbrand ("apple 6"); p.setprice (6000); p.setcolor ("white"); System.out.println (p.tostring ()); }}
4. Construction Method
An overview of how the constructor works: initializes the data for the Object.
Constructor method Format: The method name is the same as the class name, there is no return value type, and there is no specific return Value.
Construction Method Considerations: 1. If we do not provide a construction method, the system will give the default constructor method 2. If we provide a construction method, the system will no longer provide a default construction method of 3. Construction methods can also overload
/** * Mobile */class phone{ private string brand;//brand private int price;//price private string color;//color public phone () { } public phone (string brand, int price, string color) { this.brand = brand; this.price = price; this.color = color; } public void setbrand (String brand) { this.brand = brand; } public void setprice (int price) { this.price = price;&nbsP; } public void setcolor (String color) { this.color = color; } public string getbrand () { return brand; } public int getprice () { return price; } Public string getcolor () { return color; } @Override public String ToString () { return "phone{" + "brand= '" + brand + ' \ ' + ", price=" + price + ", color= '" + color + " + '} '; }}public class Phonetest { public static void main (String[] args) { phone p = new phone (); p.setbrand ("apple 6"); p.setprice (6000); p.setcolor ("white"); system.out.println (p.tostring ()); }}
5.static keywords
The static keyword can modify member variables and member METHODS.
Features of the static Keyword: 1. loaded with Class 2. Precedence over object existence 3. shared by all objects of the class
Static keyword Considerations: 1. You cannot have 2 of the This keyword in a static method. static punk can only access static member variables and static member METHODS.
Static member variables or static member methods are located in the static area of the method Area.
6. The difference between a static variable and a member variable
belong to different
A static variable belongs to a class and also becomes a class Variable.
A member variable belongs to an object, so it becomes an instance Variable.
Different locations in memory
Static variables are stored in the static area of the method Area.
Member variables are stored in heap memory.
Memory occurrence time is different
Static variables are loaded as the class loads and disappear as the class Disappears.
The member variable exists as the object is created and disappears as the object Disappears.
Call different
Static variables can be called through the class name or by an Object.
Member variables can only be called by object Name.
7.main method Format Detailed
public static void main (string[] Args) {}
Public: access is the most common, because the main () method needs to be called by the jvm, so the permissions are large Enough.
Static: not required to create the Object. It is convenient for the JVM to call through the class Name.
Void: because the return value of the method is returned to the caller, and the main () method is called by the jvm, the main () method returns Nothing.
Main: is a generic name, although not a keyword, but is recognized by the JVM.
string[] Args: previously used to receive keyboard Input.
This article is from the "11831428" blog, please be sure to keep this source http://11841428.blog.51cto.com/11831428/1856325
Encapsulation of the three main features of Java