Java class and instance, java class instance
Looking at the design pattern recently, I feel that my understanding of the three major features of java is not clear enough to understand the usefulness and advantages and disadvantages of abstract classes, interfaces, and generics. I learned half of the design pattern, and thought about it. I 'd like to stop thinking about the basics of java and start with the java object.
1. java objects
I remember when I first started learning java objects, the teacher explained to us that "Everything is an object", that is, everything is an object, objects are what you can see, feel, hear, touch, taste, or smell. To be accurate,An object is a self-contained entity, which is identified by a set of identifiable features and behaviors..
Ii. java
To explain the java class, let's start with a small example. We often pay attention to the weather forecast in our life. We abstract the weather forecast into an object, which may contain temperature, humidity, and other information, the Code is as follows:
// Temperature private int temperature; // humidity private int humidity; public int getTemperature () {return temperature;} public void setTemperature (int temperature) {this. temperature = temperature;} public int getHumidity () {return humidity;} public void setHumidity (int humidity) {this. humidity = humidity ;}
Where are the weather forecast objects? If they are in your home, would you like to get the weather forecast information from your home? The object should be placed in a proper place. Whoever needs the object will get it in a specified place, which is "class ",Class is an abstract set of objects with the same attributes and functions.. Let's look at the code. The weather forecast should be placed in the weather forecast workstation class.
// Weather forecasting workstation class public class WeatherForecastStation {// temperature private int temperature; // humidity private int humidity; public int getTemperature () {return temperature;} public void setTemperature (int temperature) {this. temperature = temperature;} public int getHumidity () {return humidity;} public void setHumidity (int humidity) {this. humidity = humidity ;}}
There are three points to note:
1. The first letter of the class name must be in upper case, and the first letter must be in upper case for multiple words.
2. The public method must use the "public" modifier.
3. For Classes modified with "public", the java file name must be the name of the class. A java file can only have one public modified class, And a java file can define multiple classes.
Iii. java instances
An instance is a real object.. For example, we are all "people", and you and I are actually "people" instances.Instantiation is the process of creating objects. You can use the new keyword class to create objects..
WeatherForecastStation station = new WeatherForecastStation ();
The above Code does two things,
WeatherForecastStation station; // declare a WeatherForecastStation object named station
Station = new WeatherForecastStation (); // instantiate the station object