We're talking JAVA When , Often say JAVA Is an object-oriented programming language , So what is object-oriented ?

Object oriented is an idea to solve problems , It mainly depends on the interaction between objects to accomplish one thing .

We can finish one thing by one , For example, we wash clothes , Will go through every step of the following : Get a basin —— to discharge water —— Put clothes —— Put washing powder —— Hand prod —— Change water —— Put washing powder —— Hand rub —— Wring dry —— hang the clothes . Of course, we can also object by object , The process of washing clothes can be simplified as : people —— clothes —— Washing powder —— Washing machine . We can find out by comparing the above two washing steps , Both process oriented and object-oriented can help us solve problems , But process oriented problem solving is complicated , Not even the missing step in the middle , And changing an object, such as washing shoes, requires another way , Obviously, it is inconvenient to solve practical problems . The process oriented approach greatly solves the problem-solving steps , It relies on the interaction between objects , In the later process, the maintenance, addition and deletion are easy to implement , These are the benefits of object orientation .

class

Since we can solve problems with object-oriented thinking , Then we need objects . What is the object ? Objects are examples of how we solve problems
, Like the people who wash clothes , Washing machine , Washing powder . When it comes to objects, we have to talk about classes
, What is a class ? for instance , We put books , pen , ruler , Eraser classified as a learning tool , They are just a class , Class is equivalent to a series of the same things with similar characteristics , Another example is people , cat , dog .... And in computers , We
Say a class as a data type , Such as integer , String type , float . But the data types here are defined by ourselves , It's kind of like C Structure in language .
As mentioned above , We are defining a class ( Custom data type ) When , We need to use its properties and behavior , such as , Define a person , We need to know the name of this person , Age , Gender , height , weight ... These are called attributes , We also need to know the behavior of this person : having dinner , sleep , study ...

So our class can be expressed in the following form ( Column by definition ):
class Class name { // attribute ( Or member variables ) String name; // name int age; // Age double weight; // weight
... // behavior ( Or method ) eat(); // having dinner sleep(); // sleep study(); // study ... }
object

It's said above , Objects are objects. Objects are instances of how we solve problems

such as :
People p1 = new People(); above p1 Is the object ( Strictly speaking, object name or object reference ,new
People() Created object space ( data ) Is the real object )
The above shows what objects and classes are , We use a column to access the following objects
public class Class_and_Object { public static void main(String[] args) { /** *
An old woman has two cats : One is called Xiaobai ,3 year , white ; * The other one is called Xiaohua ,100 year , Decor . *
Please write a program , The name of the kitten will be displayed when the user enters the kitten name , Age , colour . If the user name is entered incorrectly , It shows that the old woman doesn't have the cat */ //new
Cat() Means to create a cat ( Cat object ) //Cat cat1 = new Cat() It means to assign the created cat to cat1
//cat1 Is an object ( Strictly speaking, it is an object reference or object name ,new Cat() Created object space ( data ) Is the real object ) Cat cat1 = new
Cat(); cat1.name = " Xiaobai "; cat1.age = 3; cat1.color = " white "; cat1.weight = 10; Cat
cat2 = new Cat(); cat2.name = " floret "; cat2.age = 100; cat2.color = " Decor ";
cat2.weight = 20; // Accessing the properties of an object / behavior System.out.println(" Information about the first cat :"+cat1.name+"
"+cat1.age+" "+cat1.color+" "+cat1.weight);
System.out.println(" Information about the second cat :"+cat2.name+" "+cat2.age+" "+cat2.color+"
"+cat2.weight); } } // Create a cat class ( Create a custom data type ) class Cat{ // attribute ( member ) variable String name;
// name int age; // Age String color; // colour double weight; // weight //... // behavior //... }
Result presentation :

Technology
©2019-2020 Toolsou All rights reserved,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?