Java is an object-oriented programming language (Object Oriented Programming: OOP). There are class and object concepts in object-oriented programming language. What is the difference between their stay concepts?This is often a confusing question for object-oriented beginners. The following explanation is for the Java language, but it is common to all object-oriented programming languages. What is a class? In object-oriented programming language, a class is an abstraction of the attributes and behaviors of a class of "things". Take a simple example. PeRson is an abstraction of all the creations on the earth with special intelligence, including you, me, him, Zhang San and Li Si.
"You", "I", "He", "Zhang San", "Li Si" and so on all belong to the category of "human".
Java Classes in Language Person Definitions are often as follows: public class Person { private String name; //Attribute: Name private int height; //Attribute: Height private int weight; //Attribute: Weight public Person() {} public Person(String name, int height, int weight) { this.name = name; this.height = height; this.weight = weight; } //... some methods... public void doSth() { //Behavior: //... do something } }
What is an object?
Simply put, objects are concrete individuals of classes. For example, Zhang San is an object like Person. Person may have countless objects (just like billions of people on Earth).
In Java, object creation can use new to create an object. For example:
Person zhangsan = new Person("Zhangsan", 170, 65); // Create an object: "Zhangshan"
zhangsan.doSth(); // object behavior: the object emits its own behavior
Relationships between classes and objects
Differences between classes and objects
1. Class is an abstract concept. It does not exist in real time/space. Class only defines abstract attributes and behaviors for all objects. Just like Person, it can contain many individuals, but it does not exist in the real world.
2. Objects are a concrete part of classes. It is a real thing.
3. Class is a static concept. Class itself does not carry any data. When no objects are created for a class, the class itself does not exist in memory space.
4. Object is a dynamic concept. Each object has its own unique attributes and behaviors which are different from other objects. The attributes of an object can change with its own behavior.
Define the basic format of the class:
[modifier] class class name { // Zero to multiple constructors... // Zero to multiple member variables... // Zero to multiple methods... // Zero to multiple initialization blocks... } The modifier can be public final abstract or not written Class Name: Naming of the Great Hump
I. Constructors
Constructors are also called constructors or constructors. Constructors have the same class name, no return value, and even void cannot be written.
/* * Constructor Definition Format: [Modifier] class name (parameter list) { //Constructor Method Body Code } Constructor modifier public private protected */ public ClassDemo() { System.out.println("Parametric constructor"); } public ClassDemo(int i){ System.out.println("Constructor of a parameter"); }
(1) The name is the same as the class name, there is no return value, and void cannot be written.
(2) If no constructor is added manually in the class, the compiler will add another parameter-free constructor by default.
(3) If a constructor is added manually (in any form), the default constructor will disappear
(4) Constructors can be overloaded
(5) There are two differences in the list of parameters: the number of parameters is different and the type of parameters is different.
II. Membership variables
Membership variables: Membership variables are variables defined in a class, outside the body of a method. This variable is instantiated when an object is created. Membership variables can be accessed by methods in classes, constructors, and statement blocks of specific classes.
// Member variables // [modifier] type member variable name [= default value]; // Public protected private three choices, static, final // Instance variables int intNum; int intNum2=10; //Class variables public static String str="111";
The difference between instance variables and class variables: instance variables are used to instance new, and class variables can be invoked only by accessing their methods.
static modifies member method's most important function is to use "class name. method name" to operate method, avoiding the tedious and resource consumption of new object first.
Three, method
Java methods are collections of statements that together perform a function.
(1) The method is an orderly combination of steps to solve a class of problems.
(2) Methods are contained in classes or objects
(3) Methods are created in programs and referenced elsewhere
/ / method /* * [modifier] method return value type method name (parameter list) {// method body code [return return return return value;]} modifier (public) * protected private) three choices: static, final, synchronize, native */
IV. Creating Objects
Objects are created based on classes. In Java, use the keyword new to create a new object. Creating objects requires three steps:
(1) Declarations: Declare an object, including its name and type.
(2) Instance: Create an object by using the keyword new, which only opens up space for the object in memory.
(3) Initialization: When an object is created with new, the constructor is called to initialize the object, and the value in the object is assigned the initial value.
/** * * @author en *Class definition *[Modifier] class class name *{ * Zero to multiple constructors... * Zero to multiple member variables... * Zero to multiple methods... *} *The modifier can be public final abstract or not written *Class name: capital letter, hump name */ public class SimplePerson { //constructor /* * [Modifier] constructor name (parameter list) * { * //Constructor Method Body Code * } * Modifiers can be public private protected or not written * * The name is the same as the class name, there is no return value, and void cannot be written * Constructors can be overloaded * If no constructor is added manually in the class, the compiler will add another parameter-free constructor by default. * If a constructor is added manually (in any form), the default constructor will disappear * */ public SimplePerson(){ } //Member variables /* * [Modifier] type member variable name [= default value]; * Public protected private three choices, static, final */ String name; int age; //Method /* * [Modifier] method return value type method name (parameter list) * { * //Method body code * } * Public protected private three choices, static, final */ public void say(String text){ System.out.println(this.age +"Year old"+this.name+"Said:\""+text+"\""); } } public class Demo1 { public static void main(String[] args) { //Declared object SimplePerson simplePerson; //Call the SimplePerson class constructor through the new keyword to return a SimplePerson instance simplePerson=new SimplePerson(); //Generally, statements and new are written together. SimplePerson person=new SimplePerson(); person.age=20; person.name="Xiao Ming"; person.say("I don't know what to say."); } }
5. this refers to the reference to the current object
public class Person { String name; int age; static int intA; //Constructor overload: same as class name //Constructor overload: different parameter lists public Person(){ System.out.println("Parametric-free construction method"); this.age=20; this.name="Xiao Ming"; } public Person(int age){ this.age=age; this.name="Xiao Ming"; System.out.println("Parametric construction method 1"); } public Person(int age,String name){ this(age);//Call another constructor this.name=name; System.out.println("Parametric construction method 2"); } public Person(String name,int age){ this(age,name); System.out.println("Parametric construction method 3"); }
Overload of methods
(1) Overloading is a class with the same method name and different parameters. The return types can be the same or different.
(2) Each overloaded method (or constructor) must have a unique list of parameter types.
(3) The most common place is the overload of the constructor.
//Method overload /* * The precondition of overloading is that the caller is the same, the method name is the same, and the list of parameters is different. * The overloaded method must have different list of parameters (different number or type of parameters); * The overloaded method can change the return type. * Overloaded methods can change access modifiers; * Overloaded methods can declare new or broader check exceptions; * Methods can be overloaded in the same class or in a subclass. * The return value type cannot be used as a criterion for distinguishing overloaded functions. * Modifiers do not affect method overload */ */ /* * Three elements of a method: caller (object, class), method name, and parameter list */ public void say(String text){ System.out.println(this.age +"Year old"+this.name+"Said:\""+text+"\""); } public String say(String text,String state) { return this.age +"Year old"+this.name+state+"Said:\""+text+"\""; } String say(String text,String state,String location){ return this.age +"Year old"+this.name+"stay"+location+""+state+"Said:\""+text+"\""; } public static void Say(String text){ System.out.println(text); } public static void Say1(String text){ System.out.println(text); } } //Calling overloaded methods public class Demo2 { public static void main(String[] args) { Person person = new Person(); // person = new Person(10, "Xiaoming"); person.say("I don't know what to say."); System.out.println(person.say("I don't know what to say.","depressed")); System.out.println(person.say("I don't know what to say.","depressed","Classroom")); } }