This is the 10th day of my participation in Gwen Challenge
Friends, xiaobai’s grandmother was suddenly interested in programming after learning three characteristics of object orientation last time.
She went back and asked her little white to buy her some programming books, but she still couldn’t remember classes and objects.
How does that work? Classes and objects are the cornerstone of object-oriented programming. Must understand!
So grandma White knocked at the door again…
Well, grandma is so studious, I will tell you directly this time.
This article focuses on the story of Wang’s transformation into a manufacturing shoe factory to show and tell.
What are classes and objects?
Our hero this time is Mr. Wang. However, as a result of the housing business cold feel, he changed to open a large shoe factory, tide shoes brand, ready to upstream and downstream one-stop neat all.
Before the new coronavirus, Wang could produce 1 million pairs of shoes a day and sell them like hot cakes. However, the number of orders has been much lower since the new coronavirus.
So he called in professional business consultants. Walk into the factory a look, you here dozens of shoes are old style to design more new.
Little white grandma listened to is anxious and confused, see I did not want to stop to explain the concept.
Direct interrupt explain, “thunder committee, excuse me pull along along this close what matter?”
I helped his glasses, words are less than three! This just gets to the point. (Yes, Lei wears glasses, the rest is fake!)
‘Class’ is what business consultants call a new style. Multiple new styles are multiple ‘classes’. Shoe manufacturers, for example, have to make moulds for different types, and then mass-produce shoes from moulds. Classes are computer representations of entities in the real or mental world that encapsulate data and the operations on that data.
White grandma said oh, that new shoes is not a new class ah?
Lei shook his head, this is not a class, the new shoes from the style of the model done, is not a class, but the object!
Xiaobai grandma said again, but the size number is not the same ah, you just said different type said class? That’s a contradiction.
I shook my head. They were the same style.
You see that the shoe is an object, and the object has its own shoe size, color, etc., but it has a unique number. An object is a variable with a class type, a concrete instance of a class.
The salesman also manages by this number and marks each pair of shoes as sold!
“Said grandma, as if she understood a little. Is a shoe mold or design equivalent to a class, where the shoe is an object?
Grandma mumbled something to herself for a while. Yeah.
Can you tell me the difference? I now feel that class and object one is talking about the concept of shoe style and one is talking about a physical pair of shoes. (The school committee thinks that grandma’s understanding ability is as good as that of young people.)
The difference between classes and objects
Class is universal and refers to a similar group; The object has particularity and is the only individual
A class is a generalization of what is universal. For example, the mold of some products is a class. It’s a general description of a whole class of things that an object is instantiated, an instance of a class, like a shoe made out of a mold, can only be a concrete individual.
Here’s the code: The Shoe class is created with several brands named XAnta, X361Du, and XLeixw. There are also various styles subdivided under these big brands.
The concept of XAnta shoes we see is a category, and XAnta spring shoes is a branch of this category.
What consumers end up getting their hands on is a pair of XAnta spring Size 39, silver-gray chic, serial number (SN1639705018), which is an object.
Grandma is very happy now: “So it is so simple, quite specific! “
Ok, let’s look at the code implementation of the class below:
package org.levintech.javademo.leixuewei.classobject;
/ * * *@authorLevin * Created on 2021 1/6/20 * CSDN/Juejin Code Demo */
public class Shoe {
private int size;
private String color;
public Shoe(int size, String color) {
this.size = size;
this.color = color;
}
public int getSize(a) {
return size;
}
public String getColor(a) {
return color;
}
@Override
public String toString(a) {
return "Shoe{" +
"size='" + size + '\' ' +
", color='" + color + '\' ' +
", serialNo='" + hashCode() + '\' ' +
'} '; }}/** * XAnta */
class XAntaShoe extends Shoe {
private String brand = "XAnta";
public XAntaShoe(int size, String color) {
super(size, color);
}
@Override
public String toString(a) {
return "XAntaShoe{" +
"size='" + getSize() + '\' ' +
", color='" + getColor() + '\' ' +
", serialNo='" + hashCode() + '\' ' +
", brand='" + brand + '\' ' +
'} '; }}/** * X361Du */
class X361DuShoe extends Shoe {
private String brand = "X361Du";
public X361DuShoe(int size, String color) {
super(size, color);
}
@Override
public String toString(a) {
return "X361DuShoe{" +
"size='" + getSize() + '\' ' +
", color='" + getColor() + '\' ' +
", serialNo='" + hashCode() + '\' ' +
", brand='" + brand + '\' ' +
'} '; }}/** * XLeixw */
class XLeixw extends Shoe {
private String brand = "XLeixw";
public XLeixw(int size, String color) {
super(size, color);
}
@Override
public String toString(a) {
return "XLeixw{" +
"size='" + getSize() + '\' ' +
", color='" + getColor() + '\' ' +
", serialNo='" + hashCode() + '\' ' +
", brand='" + brand + '\' ' +
'} '; }}Copy the code
There are four classes defined above. The big Shoe class has two characteristics: size and color. And then this class XAntaShoe has its own brand, as do the other two classes. Each class then overrides the toString method and prints out the shoe information, as well as the brand information for footwear such as XAntaShoe/X361Du/XLeixw.
The code to run the sample program is as follows. :
public class LeiXueWeiRunner {
public static void main(String[] args) {
System.out.println("[Thunder Committee code Demo]");
Shoe anta01 = new XAntaShoe(40."black");
println(anta01);
Shoe x36101 = new X361DuShoe(40."blue");
println(x36101);
System.out.println(anta01 == x36101);
// Prints the hashcode for each pair of shoes
System.out.println(anta01.hashCode());
System.out.println(x36101.hashCode());
// Create two pairs of shoes of the same size and color, we can notice two pairs of shoes of the same size and color, the serial number is different!
println(new XAntaShoe(40."black"));
println(new XAntaShoe(40."black"));
// Create anta04 with the same color, style and size as Anta04 to see if it is the same object
Shoe anta04 = new XAntaShoe(40."black");
System.out.println("Is ANta01 the same as ANta04?" + (anta01 == anta04));// This judgment is referential
System.out.println("Is ANta01 the same as ANta04?" + (anta01.equals(anta04)));// The object is the same
}
public static void println(Shoe shoe) {
System.out.println("Shoes."+ shoe); }}Copy the code
The operation effect picture is as follows:
This is the particular object that you’re printing. We see that each shoe has a serial number (serialNo) and is unique.
Classes are stateless, objects are stateful and have concrete values!
Whenever you create a new object, regardless of the style and color of the shoe, the serial number will change.
Furthermore, we determine whether Anta01 and Anta04 are the same objects (same shoes). The answer is: no matter the class instance attribute values are the same, the system still considers them not the same object!
Classes are persistent, whereas objects exist only in memory and are destroyed at the end of program execution!
We will not be able to retrieve the object after the above example code runs.
But the class still exists on the current system’s disk, as shown in the.class file (the bytecode file for the class).
conclusion
This article covers classes and objects
- Class universal reference meaning
- The particularity of object, object is a unique individual
- The difference between classes and objects
The large shoe factory above is like a Java virtual machine (JVM), constantly making shoes from molds (.class files) (creating object), and then selling shoes.
Language (Java) is just used to express, the core principles of other languages are applicable, brush millions can not buy this value!
First said here, later will talk about, pay attention to long-term reading => Lei Xuewei interesting programming story compilation
Easter egg: In practice, in some scenarios, we will consider objects with equal attributes to be equal. Lei Committee has rewritten the relevant method in XLeixw class. Readers can download the code at the end of the article and try to create two XLeixw shoes with the same color and size to determine whether they are the same.
Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Welcome to pay attention, and feel free to like the collection!
Code link: CSDN Repo Gitee