当前位置:网站首页>Class and object learning
Class and object learning
2022-06-26 05:50:00 【Wang Xiaoya】
What are objects and classes ?
Everything is object , Everything that exists objectively is the object
class : Class is a class in real life Having common attributes and behaviors Of things abstract
object : It can be seen and touched Real existence Of Entity
A class is an abstraction of an object
Object is an entity of a class
attribute : The various characteristics of objects , Every... Of every object attribute Have specific value
Behavior : Object can perform
Class characteristics :
- Class is the data type of an object
- Class is a collection of objects with the same properties and behaviors
For example, the concept of mobile phone is a class , Because mobile phones have common attributes : How much memory 、 The screen size 、 The price is high and low 、 brand ……
There are also common behaviors : You can take and call 、 Can send and receive SMS 、 You can listen to music ……
The definition of a class
The importance of class : yes Java The basic unit of a program
What is the class : It is a kind of Common properties and Behavior The abstraction of things , Determine the properties and behavior that the object will have .
Class composition : attribute and Behavior
- attribute : Pass in class Member variables To embody ( Variables outside methods in a class )
- Behavior : Pass in class Member method To embody ( Compared with the previous method, remove static Key words can be used )
Class definition steps :
publicclass Class name {
// Member variables
Variable 1 Data type of Variable 1;
Variable 2 Data type of Variable 2;
…
// Member method
Method 1;
Method 2;
…
}
Class name : mobile phone (Phone)
Member variables : brand (brand) Price (price)
Member method : Make a phone call (call) texting (sendMessage)
package object;
/* Mobile phones :
Class name :
mobile phone (Phone)
Member variables :
brand (brand)
Price (price)
Member method :
Make a phone call (call)
texting (sendMessage)*/
// Class definition steps ;
// Defining classes
public class Phone {
// Write member variables of a class
String brand;
int price;
// Write member methods for a class
public void call(){
System.out.println(" Make a phone call ");
}
public void sendMessage(){
System.out.println(" texting ");
}
}
Use of objects
Create objects
Format : Class name Object name = new Class name ();
Example :Phone p = new Phone();
Use object
1: Use member variables
- Format : Object name . Variable name
- Example :p.brand
2: Use the member method
- Format : Object name . Method name ()
- Example :p.call()
Case study :
package object;
public class PhoneDemo {
public static void main(String[] args) {
/* Create objects
Format : Class name Object name = new Class name ();*/
Phone p = new Phone();
/* Use object
1: Use member variables
Format : Object name . Variable name */
System.out.println(p.brand);
// Call variables brand, Because not assigned , At this point, the default heap memory value is returned ,String Type default value “null”
System.out.println(p.price);
//price ditto ,int Type default value “0”
// If the assignment is
p.brand = " Huawei ";
p.price = 2999;
// Once again, the output
System.out.println(p.brand);
System.out.println(p.price);
/*2: Use the member method
Format : Object name . Method name ()*/
p.call();
p.sendMessage();
}
}
Output results :
null
0
Huawei
2999
Make a phone call
texting
demand : Define a student class first , Then define a student test class , Complete the use of member variables and member methods through objects in the student test class
analysis :
Member variables : full name , Age …
Member method : Study , do the homework …
Ideas :
Class name :Student
Member variables :name,age
Member method :study(),doHomework()
Class name :StudentDemo
Because I have to do a test , So there is a main method :main Method
Assign a value to a member variable , Output the value of the member variable
Calling a member method
package com.object_02;
// Create a student class
public class Student {
// Member variables
String name;
int age;
// Member method
public void study(){
System.out.println(" study hard , Day day up ");
}
public void doHomework(){
System.out.println(" The keyboard is broken , The monthly salary is over ten thousand ");
}
}
package com.object_02;
// Student tests
public class StudentDemo {
public static void main(String[] args) {
// Create objects
Student s = new Student();
// Use object ( The default value is output )
System.out.println(s.name+","+s.age);
// assignment
s.name = " Chen Rui ";
s.age = 44;
// Once again, the output
System.out.println(s.name+","+s.age);
// Usage method
s.study();
s.doHomework();
}
}
边栏推荐
猜你喜欢
Learn cache lines and pseudo sharing of JVM slowly
转帖——不要迷失在技术的海洋中
REUSE_ALV_GRID_DISPLAY 事件实现(DATA_CHANGED)
10 set
5 minutes to learn regular expressions
通俗易懂的从IDE说起,再谈谈小程序IDE
[arm] build boa based embedded web server on nuc977
pytorch(网络模型训练)
pytorch(环境、tensorboard、transforms、torchvision、dataloader)
冒泡排序(Bubble Sort)
随机推荐
Leetcode114. Expand binary tree into linked list
Force buckle 875 Coco, who likes bananas
What management systems (Updates) for things like this
Something about MariaDB
使用Jedis監聽Redis Stream 實現消息隊列功能
Gram 矩阵
操作符的优先级、结合性、是否控制求值顺序【详解】
Thinking about bad money expelling good money
Lesson 4 serial port and clock
冒泡排序(Bubble Sort)
DOM文档
状态模式,身随心变
Redis discovery bloom filter
Security problems in wireless networks and modern solutions
虚拟项目失败感想
A new explanation of tcp/ip five layer protocol model
Feelings of virtual project failure
The State Council issued a document to improve the application of identity authentication and electronic seals, and strengthen the construction of Digital Government
Daily production training report (15)
bingc(继承)