当前位置:网站首页>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();
}
}

边栏推荐
- Force buckle 875 Coco, who likes bananas
- [PHP] PHP two-dimensional array is sorted by multiple fields
- Kolla ansible deploy openstack Yoga version
- [C language] deep analysis of data storage in memory
- Gram 矩阵
- Thinking about bad money expelling good money
- cross entropy loss = log softmax + nll loss
- 适配器模式
- Leetcode114. Expand binary tree into linked list
- Using Jenkins to perform testng+selenium+jsup automated tests and generate extendreport test reports
猜你喜欢

【 langage c】 stockage des données d'analyse approfondie en mémoire

原型模式,咩咩乱叫

kolla-ansible部署openstack yoga版本

通俗易懂的从IDE说起,再谈谈小程序IDE

Leetcode513. Find the value in the lower left corner of the tree

How to use the tablet as the second extended screen of the PC

AutowiredAnnotationBeanPostProcessor什么时候被实例化的?

Sofa weekly | open source person - Yu Yu, QA this week, contributor this week

Ribbon load balancing service call
![[C language] deep analysis of data storage in memory](/img/2e/ff0b5326d796b9436f4a10c10cfe22.png)
[C language] deep analysis of data storage in memory
随机推荐
How to use the tablet as the second extended screen of the PC
[MySQL] MySQL million level data paging query method and its optimization
String类学习
花生壳内网穿透映射NPM私服问题
Adapter mode
When was the autowiredannotationbeanpostprocessor instantiated?
The model defined (modified) in pytoch loads some required pre training model parameters and freezes them
Machine learning 07: Interpretation of PCA and its sklearn source code
Lesson 4 serial port and clock
5分钟包你学会正则表达式
Uni app ceiling fixed style
FindControl的源代码
BOM文档
Kolla ansible deploy openstack Yoga version
DOM document
About abstact and virtual
睛天霹雳的消息
循环位移
423- binary tree (110. balanced binary tree, 257. all paths of binary tree, 100. same tree, 404. sum of left leaves)
怎么把平板作为电脑的第二扩展屏幕