1, encapsulation of
The connection between an object and the outside world should be exposed through a unified interface that should be exposed and hidden.
Encapsulation of attributes: The default value for access to the properties of a class in Java is defaults, and to hide the property or method, you can add private (private) modifiers to restrict access only to the inside of the class. For a private property in a class, it is given an opposite method (GetXxx (), setxxx ()) to access the private property, guaranteeing the security of the operation of the private property.
encapsulation of the method: for the encapsulation of the method, the exposed public, the hidden hidden. method exposes the declaration (definition) of a method (which can be called only if the parameter and the return value are known), and the implementation of the hidden method minimizes the effect of the implementation change on the schema.
123456789101112131415161718192021222324252627282930 |
public
class
TestDemo {
public
static
void main(String[] args) {
Person person=
new
Person();
person.tell();
person.setAge(-
20
);
person.setName(
"张三"
);
person.tell();
}
}
class
Person{
private int
age;
private
String name;
public
int
getAge() {
return
age;
}
public
void
setAge(
int
age) {
if
(age>
0
&&age<
150
){
this
.age = age;
}
}
public
String getName() {
return
name;
}
public
void setName(String name) {
this
.name = name;
}
void
tell(){
System.out.println(
"姓名:"
+name+
";年龄:"
+age);
}
}
|
Note:
(1) Java Anonymous Object : A place to use only once. For example: New person (). tell ();
(2) Java Constructor Method : The constructor method name must be the same as the class name, no return means, can be overloaded, primarily for the properties in the class initialization.
(3) the data type that the value is passed : eight basic data Types and string (string is also the address that is passed, except that the string object and other objects are different and the string is final, so the value of the string object cannot be changed, A change in value will result in a new object. Then StringBuffer can, but only change its contents, cannot change the memory address that the external variable points to.
The data type passed by reference : All composite data types except string, including arrays, classes, and interfaces.
123456789101112131415161718192021222324252627282930 |
public
class
TestRef4 {
public
static
void main(String args[])
{
int
val=
10
;;
StringBuffer str1, str2;
str1 =
new
StringBuffer(
"apples"
);
str2 =
new
StringBuffer(
"pears"
);
System.out.println(
"val:"
+ val);
System.out.println(
"str1 is "
+ str1);
System.out.println(
"str2 is "
+ str2);
System.out.println(
"..............................."
);
modify(val, str1, str2);
System.out.println(
"val is " + val);
System.out.println(
"str1 is "
+ str1);
System.out.println(
"str2 is "
+ str2);
}
public
static void
modify(
int
a, StringBuffer r1,StringBuffer r2)
{
a =
0
;
r1 =
null
;
r2.append(
" taste good"
);
System.out.println(
"a is "
+ a);
System.out.println(
"r1 is "
+ r1);
System.out.println(
"r2 is "
+ r2);
System.out.println(
"..............................."
);
}
}
|
The output is:
1234567891011 |
val: 10 str1 is apples str2 is pears ............................... A&NBSP;IS&NBSP; 0 R1&NBSP;IS&NBSP; null r2 is pears taste good VAL&NBSP;IS&NBSP; 10 str1 is apples str2 is pears taste good |
(4) This keyword : Represents a property or method in a class, a constructor method in a calling class, for example: this (), or the current object.
(5) Static keyword : Static declaration property is a global property; The static declaration method is called directly through the class name; I read the Java Video tutorial that says: When you use the static method, You can access only the properties and methods of the static declaration, not the properties and methods of the static declaration.
2, the Inheritance of
In a program, you can use the extends keyword to let one class inherit another class, the inherited class is a subclass, the inherited class is the parent class, and the subclass automatically inherits all the methods and properties of the parent class
Java Object-oriented