Recommended courses: Java tutorial
1. First, we need to figure out what a class is? What is an object?
Class - is abstract. It is an abstraction (or collection) of objects with common attributes and services, which includes two main parts: attributes and services;
Object - is concrete. It is an entity used to describe objective things in the system. It is a basic unit that constitutes the system.
The relationship between classes and objects is like the relationship between molds and castings. The instantiation result of a class is an object, and the abstraction of a type of object is a class.
2. Definition of class
The general format for defining a class in Java: modifier class class name {member}
Understanding: The modifier is Optional, with public or not;
class——keyword, must have;
class name—— The first letter is capitalized, and the name is based on the camel case naming rule. It must have;
members - it has member attributes and member methods, which will be explained in detail below.
3. Member (fields) definition
1. Member attribute definition
格式:[ public | private | protected ] [ static ] [ final ] 类型 成员名
Understanding: Type and member name are Required, others are optional;
public public members - can also be accessed by different packages;
private private members - only Can be accessed in this class;
protected protected members - accessible in the same package and subclasses;
Do not add the above three Default - accessible in the same package;
static
Static member - belongs to the class, it does not belong to a specific object, and all objects of the class share this member. All objects that are not static objects must be instantiated before they can be accessed.
Static members or methods are accessed directly through the class name (via: class name. reference);
final member - the final member, whose The value cannot be changed;
final class - the final class, cannot be inherited;
final method - the final method, this method cannot Rewritten;
Note: final and public can be loaded on the class, but static cannot.
2. Member method definition
格式:[public|private|protected] [static] [final] 类型 方法名(形参){ 方法体; return表达式; }
Understanding:
Type (first letter capitalized) (required): refers to the type of return value (either It is a basic type, or it can be an object type);
Method name (required): what to do;
Method body: defines the method The specific content usually plays two roles: one is to perform various operations around the attributes of the class, and the other is to perform data exchange and message passing operations with other classes and objects;
Formal parameters (optional): There are only two types: basic type and object type. Parameters in method calls are used to pass values and references. At the same time, methods can also be nested and called recursively;
return can not only return a value, but also end the method body. It is worth noting Yes: If a non-void return type is specified in the method body, the method must include a return statement to ensure that a value is returned under any circumstances. The return statement cannot be followed by any expression.
Note: Defining another method in a method will produce a syntax error (methods cannot be nested, classes can). It is best to avoid local variables "shielding" instance (member) variables, which are not defined in a class. This can be done by using an identifier with the same name.
If the member variable with the same name is blocked and you want to access the member variable again, you can use this.member variable
.
Understanding:
Local variables: variables declared or defined in a method. Variables are created when this method is called and when this method is executed. Variables are released;
Instance variables: variables declared or defined outside all methods but in the class body, the valid scope is the entire class;
Shielding is not overwriting
4.Now a class is created. Of course, you can define a class that only contains member variables or only methods. If If a class has only member variables, then this class cannot do anything. In order for the class to do things, methods must be defined for this class. Because class methods can be called by other classes through objects, they are also called interfaces of classes.
5. A brief example is as follows:
public class Student { //创建了一个名为Student的公共类 public String name;//定义name属性 在String前加上public,则其他包也能调用此属性,不加则无法调用 public void study(){ System.out.println("我是学生"+name+",正在学习"); } //写一个公共方法study() public static void hi(){ System.out.println("hi,everybody!"); } //写一个静态的公共方法hi() }
The above is the detailed content of How to create a java class. For more information, please follow other related articles on the PHP Chinese website!