7. member variables and local variables
Member variables: defined in a class to describe what the object will be
Local variables: defined in a method of a class to hold temporary data in a method
Difference: scope is different
The scope of a local variable is limited to the method that defines it
The scope of a member variable is visible within the entire class
8. encapsulation and concealment of information
In Java, by declaring the data private , the publicmethod is available : GetXXX and the setxxx The operation of this property is implemented to achieve the following purposes:
Hides the implementation details of a class;
The user can only access the data through the pre-custom method, it is convenient to add the control logic and restrict the unreasonable operation of the attribute.
Easy to modify, enhance the maintainability of the Code;
Example:
public class animal{
private int legs; Define attribute legs as private
public void Setlegs (int i) {
if (i! = 0 && I! = 2 && I! = 4) {
System.out.println ("Wrong number of legs!");
Return
}
Legs=i;
}
public int Getlegs () { defines the method here
return legs;
}
}
9. definitions and functions of constructors
Characteristics of the construction method
when an instance object of a class is first generated, the constructor of this class is automatically called, and we can add the code to do the initialization work in this method.
Characteristics of the construction method
Same as the class name;
no return value ( differs from void)
Example:
public class Animal {
private int legs;
Public Animal () {legs = 4;} constructor
public void Setlegs (int i) {legs = i;}
public int Getlegs () {return legs;}
}
Another: The default constructor method:
In the Java language, each class has at least one construction method;
if the class's definition does not explicitly define any of the constructor methods, the system automatically provides a default parameterless construction method. Therefore: A class itself does not write a constructor method, and can still create an instance of the class with new Xxx ().
Java class, once the class's definition explicitly defines one or more construction methods, the system will no longer provide the default construction method;
The main role of the constructor is to initialize the properties of the object with the constructor parameters.
10.
One. overloading of methods
the overload of a method is to allow more than one method of the same name to exist in the same class, as long as they have a different number of arguments or types. such as:
public class test{
public static void Main (String [] args) {
int isum;
Double DSum;
Isum=add (3,5);
Isum=add (3,5,6);
Dsum=add (3.2,6.5);
}
public static int Add (int x,int y) {return x+y;}
public static int Add (int x,int y,int z) {return x+y+z;}
public static double Add (double x,double y) {return x+y;}
}
Specific requirements:
multiple methods with the same name can be defined in the same class
public class printstream{
public void print (int i) {... }
public void print (float f) {... }
public void print (String s) {... }
}
the parameter list for overloaded methods must be different
The return value types of overloaded methods can be the same or different
the invocation is distinguished by the parameter type of the method.
Print (3);print (1.2f);print ("hello! " )
overloading of construction methods
Construction methods are typically used to initialize objects while creating objects. Such as
Class person{
String name;
int age;
Public person (String N, int a) {name=n; age=a;}
}
Construction method overloading makes the creation of objects more flexible and facilitates the creation of different objects.
Example of constructing method overloads:
public class person{
Public person (String name, Int. age, Date D) {this (name,age);}
Public person (String name, int age) {... }
Public person (String name, Date d) {... }
Public person () {... }
}
Constructor method overload, parameter list must be different
Example:
public class Person {
private String name;
private int age;
Private Date birthDate;
Public person (String name, int age, Date d) {
THIS.name = name;
This.age = age;
This.birthdate = D;
}
Public person (String name, int age) {
This (name, age, NULL);
}
Public person (String name, Date d) {
This (name, +, D);
}
Public person (String name) {
This (name, 30);
}
}
13.this Keywords
inside each member method, there is a this reference variable that points to the object that called the method.
See the procedure above for specific examples
Java Summary second time (remainder)//class and Object 1