day06-面向对象-引用类型对象作为方法参数

This commit is contained in:
2026-01-19 09:54:43 +08:00
parent 28a022fb48
commit 54e8600789
2 changed files with 83 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
package com.inmind.object01;
//对面向对象-引用类型对象作为方法参数
public class Demo03 {
public static void main(String[] args) {
//买一台星空黑的尺寸5.5价格4999的小米手机
Phone p1 = new Phone();
p1.brand = "小米";
p1.price = 4999;
p1.size = 5.5;
p1.color = "星空黑";
//展示下对应手机的属性
/*System.out.println(p1.brand);
System.out.println(p1.price);
System.out.println(p1.size);
System.out.println(p1.color);*/
showPhone(p1);
//买一台土豪金的尺寸6.1价格8999的苹果手机
Phone p2 = new Phone();
p2.brand = "苹果";
p2.price = 8999;
p2.size = 6.1;
p2.color = "土豪金";
//展示下对应手机的属性
/*System.out.println(p2.brand);
System.out.println(p2.price);
System.out.println(p2.size);
System.out.println(p2.color);*/
showPhone(p2);
}
//在非描述类的类中定义方法一般加上static
//定义一个方法,接收一个手机,展示它的属性
public static void showPhone(Phone phone) {//引用数据类型,作为参数
//展示下对应手机的属性
System.out.println(phone.brand);
System.out.println(phone.price);
System.out.println(phone.size);
System.out.println(phone.color);
}
}

View File

@@ -0,0 +1,40 @@
package com.inmind.object01;
/*
该类用来描述一个手机类
属性(成员变量)和行为(方法)
品牌
价格
颜色
尺寸
行为:
打电话
发短信
玩app
*/
public class Phone {
//品牌
String brand;
//价格
double price;
//颜色
String color;
//尺寸
double size;
//打电话
public void call(String number) {
System.out.println(""+number+"打电话");
}
//发短信
public void send(String number) {
System.out.println(""+number+"发短信");
}
//展示当前手机信息
public void show(){
System.out.println("一台品牌为"+brand+",颜色为"+color+",价格为"+price+",尺寸为"+size+"的手机");
}
}