1 ImportJava.util.Scanner;2 3 Public classPersondemo {4 Public Static voidMain (string[] args) {5 6person P1 =NewPerson ("Xiaolei", 20);7P1.setname ("Lifang");8String newname =p1.getname ();9 System.out.println (newname);Ten One A } - } - the classperson{ - String name; - intAge ; -Person (String name,intAge ) { + -SYSTEM.OUT.PRINTLN (name + "Age is" +Age ); + cry (); A } at - - Public voidSetName (String N) { -Name =N; - } - PublicString GetName () { in returnname; - } to + Public Static voidcry () { -System.out.println ("Cry ..."); the } *}
Recently learned core Java fourth class and object parts, or feel to do a summary, because many features in learning will begin to forget, good memory than bad pen.
Java is an object-oriented language.
Class
A class is a large collection of all elements that have the same attributes, where the object template is created, and the object that is created becomes an instance of the class. For each object to be created, it will have the corresponding class, the object's attributes, the object's initialization, the method the object depends on, and so on.
There are three relationships between a class and a class
1. Dependency on a class requires access to information inside another class
2. Inheriting a class B is related to Class A, but B has a redundant function other than a, saying that Class B inherits Class A. A becomes a superclass and B is called a subclass. such as managers manager is a superclass, employee staff is a subclass. Use the Extends keyword
class extends a{}
As a result, subclasses are richer in functionality than superclass, and when designing a class, you should put a common method in a superclass and put special functions in subclasses. Use the Super keyword, such as Super.risemoney, when you need to invoke a variable that inherits a superclass from a subclass.
3. Aggregation
When implementing a class, because public data is dangerous, all data fields should be designed to be private, through the private keyword.
Object
The object has three mainfeatures:
1. The behavior of the object ———— what actions can be applied to the object and what methods can be applied to the object.
2. The state of the object ———— how the object responds when those methods are applied
3. The identity of the object ———— how to tell which properties of different object objects with the same behavior and state are defined in the class are called instance domains, methods are used to manipulate objects and to access their instance fields. After each object is created, the object is initialized with the constructor. 1. The constructor name is the same as the Class 2. Each class can have more than one constructor 3. The constructor has 0 1 or more parameter 4. The constructor does not return a value of 5. Constructors and functions can be overloaded with the new operation, overloading is the same name but different parameters, including type or number. The constructor can have an initialization block, which is a common part of each constructor. The format is a well-enclosed section of curly bracesThis keywordIf the code says so
This greatly affects the readability of the code.
Class person{ String name; int Age ; int Age ) { = name; = Age }}
So using the This keyword distinguishes between implicit and explicit parameters. An explicit parameter is a parameter within parentheses, and an implicit parameter is not passed directly to.
// This is the modified code. class person{ String name; int Age ; int Age ) { this. Name = name; this. Age = Age }}
The role of this can be understood as a reference to an object substitution. This can be used to distinguish between instance fields and local variables.
Final instance Domain
If a field cannot be modified in subsequent operations after the constructor constructs a value, the Finnal keyword is used. Private final String Name
Static domain and static method
Static keyword
A static domain is a domain in which all classes share (shared), and each domain obtains a copy here. That is, if several classes will have the same part, the operation or the power domain, then you can create a static class that puts this shared thing inside. If you do not add the static keyword, only objects of this class can be called.
Java Learning Summary 02