赞
踩
上一篇主要介绍了面向对象的思想以及内存实现,关于类与对象感觉写的不够好,因此才会有这一篇作为补充;
一些相同属性和行为的事物的统称,比较广泛、抽象,比如人类,动物类,鸟类;
含有属性和行为(可以有其中一个,也可以都有),属性一般用来描述对象的特征,行为用来描述对象可以干什么。
比如动物类中有一个对象:狗; 属性:狗有名字,体重; 行为:狗会吃饭,睡觉;
在Java中,先有类再有对象的;
- public class ClassAnimal {
- /*
- 属性:狗有名字,体重;
- 行为:狗会吃饭,睡觉;
- */
- //成员变量(属性)
-
- }
属性:狗有名字,体重;
权限修饰符 数据类型 变量名(一般不对变量名赋值,毕竟对象的属性一般不相同);
如:public String name ; // 名字属性
后面我们再说说 权限修饰符 的事情;
- public class ClassAnimal {
- /*
- 属性:狗有名字,体重;
- 行为:狗会吃饭,睡觉;
- */
- //成员变量(属性)
- public String name;
- public int weight;
-
- }
行为:狗会吃饭,睡觉;
我们先了解基本的行为的创建:权限修饰符 返回值类型 方法名
无返回值类型无形参列表:
public void eat ( ) {
System.out.println( " 爱吃骨头头 " ) ;
}
- public class ClassAnimal {
- /*
- 属性:狗有名字,体重;
- 行为:狗会吃饭,睡觉;
- */
- //成员变量(属性)
- public String name;
- public int weight;
-
- //成员方法(行为)
- public void eat(){
- System.out.println("爱吃骨头头");
- }
- public void sleep(){
- System.out.println("睡觉睡得死");
- }
- }

类的具体表现,用于调用类的 属性 和 行为 ;
对象的创建:类名 对象名 = new 类名();
如:ClassAnimal dog = new ClassAnimal ( ) ;
可以仿照 Scanner 的用法,毕竟都是引用类型 : Scanner scn = new Scanner ( ) ;
如果类与对象不在同一包下,第一步需要导包:import 包名 . 类名
- public class dog {
- public static void main(String[] args) {
- ClassAnimal dog=new ClassAnimal();
-
- }
- }
对象调用类的属性和方法:对象名 .
- public class dog {
- public static void main(String[] args) {
- ClassAnimal dog=new ClassAnimal();
- dog.name="大黄";
- dog.weight=50;
-
- dog.eat();
- dog.sleep();
- System.out.println(dog.name+"\t"+dog.weight);
- }
- }
- C:\Java\jdk-17.0.11\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=13016:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Test\classDemo\out\production\classDemo dog
- 爱吃骨头头
- 睡觉睡得死
- 大黄 50
-
- 进程已结束,退出代码0
(5)注意事项
类的 属性 和 行为 在 main 方法外创建 ;
成员变量有默认值,规则与数组相同;
定义一个手机类,有 颜色,品牌,价格的属性,有打电话,发消息的行为,创建对象并调用;
如下,为定义的手机类:
- public class ClassPhone {
- //成员变量:颜色,品牌,价格
- public String color;
- public String brand;
- public int price;
-
- //成员方法:打电话,发消息
- public void call(String who){
- System.out.println("给"+who+"打电话");
- }
-
- public void message( ){
- System.out.println("总爱发消息!");
- }
- }
如下,为创建的类的对象,并进行调用;可以看出,成员变量的默认值符合数据类型的默认值的规则;
- public class phone {
- public static void main(String [] args){
- //创建对象
- ClassPhone c=new ClassPhone();
-
- System.out.println(c.brand);//null
- System.out.println(c.color);//null
- System.out.println(c.price);//0
- System.out.println(" ");
-
- c.brand="huawei";//华为
- c.color="black";//黑色
- c.price=6999;//6999
- System.out.println(c.brand);
- System.out.println(c.price);
- System.out.println(c.color);
- c.call("刘备备");
- c.message();
- }
- }

两个对象互不干扰;
- public class phone {
- public static void main(String [] args){
- //创建对象
- ClassPhone c=new ClassPhone();
- c.brand="huawei";//华为
- c.color="black";//黑色
- c.price=6999;//6999
- System.out.println(c.brand);
- System.out.println(c.price);
- System.out.println(c.color);
- c.call("刘备备");
- c.message();
- System.out.println(" ");
- //再次创建对象
- ClassPhone c1=new ClassPhone();
- c1.brand="小米";//华为
- c1.color="blue";//黑色
- c1.price=5999;//6999
- System.out.println(c1.brand);
- System.out.println(c1.price);
- System.out.println(c1.color);
- c1.call("曹操");
- c1.message();
- }
- }

- C:\Java\jdk-17.0.11\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=6083:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Test\classDemo\out\production\classDemo phone
- huawei
- 6999
- black
- 给刘备备打电话
- 总爱发消息!
-
- 小米
- 5999
- blue
- 给曹操打电话
- 总爱发消息!
-
- 进程已结束,退出代码0
将 c 的地址赋给 c1,因此 c1 根据地址找到 c 的属性;
- public class phone {
- public static void main(String [] args){
- //创建对象
- ClassPhone c=new ClassPhone();
- c.brand="huawei";//华为
- c.color="black";//黑色
- c.price=6999;//6999
- System.out.println(c.brand);
- System.out.println(c.price);
- System.out.println(c.color);
- c.call("刘备备");
- c.message();
- System.out.println(" ");
- //再次创建对象
- ClassPhone c1=c;
- System.out.println(c1.brand);
- System.out.println(c1.price);
- System.out.println(c1.color);
- c1.call("曹操");
- c1.message();
- }
- }

- C:\Java\jdk-17.0.11\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=6228:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Test\classDemo\out\production\classDemo phone
- huawei
- 6999
- black
- 给刘备备打电话
- 总爱发消息!
-
- huawei
- 6999
- black
- 给曹操打电话
- 总爱发消息!
-
- 进程已结束,退出代码0
当一个对象作为形参传递给方法时,传递的是对象的地址;
- public class phone {
- public static void main(String [] args){
- //创建对象
- ClassPhone c=new ClassPhone();
- c.brand="huawei";//华为
- c.color="black";//黑色
- c.price=6999;//6999
- //调用方法
- method(c);
- }
- public static void method(ClassPhone c){
- System.out.println(c.brand);
- System.out.println(c.color);
- System.out.println(c.price);
- c.message();
- }
- }

- public class phone {
- public static void main(String [] args){
- //赋值调用
- ClassPhone cc=method();
- System.out.println(cc.price);//调用
- System.out.println(cc.brand);//调用
- System.out.println(cc.color);//调用
- cc.call("刘备"); //调用方法
- cc.message(); //调用方法
-
- }
- //方法的返回值类型为 对象
- public static ClassPhone method( ){
- //创建对象
- ClassPhone c=new ClassPhone();
- c.brand="小米";
- c.price=3999;
- c.color="红色";
- return c;
- }
- }

- C:\Java\jdk-17.0.11\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=8352:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Test\classDemo\out\production\classDemo phone
- 3999
- 小米
- 红色
- 给刘备打电话
- 总爱发消息!
-
- 进程已结束,退出代码0
成员变量定义在 main 方法中 ,局部变量定义在方法中;
- public class Extend {
- public static void main(String[] args) {
- String name;//全局变量
- }
- public void method(){
- int a=20;//局部变量
- //System.out.println(a);
- }
- }
成员变量在整个类中都可以使用,局部变量只能在该方法内使用;
- public class Extend {
- public static void main(String[] args) {
- int age=100;//全局变量
- System.out.println(age);
- method();//无法调用
- }
- public void method(){
- int a=20;//局部变量
- System.out.println(a);
- }
- }
局部变量在栈内存中,无默认值,必须赋值才能使用;
成员变量在堆内存中,默认值的规则与数组相同;
局部变量随方法的入栈而创建,随方法的出栈而消失;
成员变量随随对象的创建而产生,随着对象被垃圾回收而消失;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。