当前位置:网站首页>类与对象
类与对象
2022-07-17 05:22:00 【INSIGNER】
类class
- 类是对象的抽象,描述了对象的基本类型。如每个人有年龄、名字,会吃饭、睡觉、打游戏。类就是将这些,人的共同特征用抽象的放法表现出类。
- 类也是,面向对象编程语言,实现面向对象的基本单位。一个完整的Java程序至少包含一个类。
类的定义
定义一个:
//定义一个表示人的类
public class Person{
int age; //人有年龄
String name; //人有名字
public void sleep(){
//睡觉的方法
System.out.print("睡觉");
}
public void eat(){
//吃饭的方法
System.out.println("吃饭");
}
public void playGame(){
//打游戏的方法
System.out.println("打游戏");
}
}
- 在同一个源文件中可以包含多个类,但只有一个类可以使用public来修饰。
对象
对象可以说是,类的实体化表现。如果说类是一张蓝图,对象就是类的具体实现。人的蓝图构建的对象就是有名字、有年龄的不同的人。
定义一个对象:
public class Persons{
public static void main(String[] args) {
Person person = new Person();
person.name = "张三";
person.eat();
}
}public class Persons{
Person zhangsan = new Person();
zhangsan.name = "张三";
}
边栏推荐
- Generate audio and waveform in PWM and DAC exercises of stm32
- OLED data display based on spi/iic interface
- Good partner of single chip microcomputer - CS Genesis SD NAND flash
- 小迪网络安全-笔记(5)
- Performance test and price comparison of cloud servers of Alibaba cloud, Tencent cloud, Huawei cloud, ucloud and Tianyi cloud
- How to download free papers from CNKI
- Xiaodi network security - Notes (4)
- Talk about Zhongtai: my understanding and thinking about Zhongtai
- Galaxy Kirin server system building local and LAN Yum source
- Gnome 安装扩展插件(40.1版本,opensuse tumbleweed)。
猜你喜欢
银河麒麟桌面操作系统V10SP1(X86)安装SQL Developer
天翼云 杭州 云主机(VPS) 性能评测
Xiaodi network security - note encryption coding algorithm (6)
银河麒麟服务器系统搭建本地和局域网yum源
Tcp/ip protocol learning
Performance evaluation and comparison of Huawei cloud Kunpeng arm ECs and x86 ECS
IP103.53.125.xxx IP地址段 详解
Arm server building my world (MC) version 1.18.2 private server tutorial
What is data center?
小迪网络安全-笔记(3)
随机推荐
我的世界 1.18.1 Forge版 开服教程,可装MOD,带面板
轻重搭配(贪心)
Minecraft bedrock BDS service tutorial
文本三剑客之awk命令--截取
递归访问目录,打印斐波那契数列,高阶函数
Release nohup Out disk space occupied
F5 GTM (I): DNS parameters
Escape from the center of the lake (math problem)
IP fragment是什么意思?如何防御IP fragment攻击?
我的世界1.12.2 神奇宝贝(精灵宝可梦) 开服教程
[ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
Judge concave convex (mathematical problem)
103.53.124.X IP段BGP线路和普通的专线有什么区别
Openssl--- stack
How to set primary key self growth in PostgreSQL database
Freebsd 12更换启动界面背景
X11 forwarding
ssh远程登录服务
汉诺塔2(函数)
Technical specification for secure electronic signature and password gm/t 0031 | GB / T 38540 format OpenSSL package analysis