栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

面向对象练习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

面向对象练习

* 每一款手机都有自己的品牌和价格,原来的手机只能打电话,发短信;现在的新手机,
    在打电话和发短信的基础上还能玩儿游戏,请设计程序,完成手机的升级!

public class Test {
	public static void main(String[] args) {
		//创建新手机对象
		NewPhone phone=new NewPhone("小米",1999);
		System.out.println("新手机的品牌为:"+phone.getBrand()+" "+"新手机的价格为"+phone.getPirce());
		phone.call();
		phone.takeMessage();
		phone.playGame();
		
	}
}
public class Phone {
	private String brand;
	private int pirce;
	
	public Phone(String brand, int pirce) {
		super();
		this.brand = brand;
		this.pirce = pirce;
	}

	public Phone() {
		super();
		// TODO Auto-generated constructor stub
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public int getPirce() {
		return pirce;
	}

	public void setPirce(int pirce) {
		this.pirce = pirce;
	}

	@Override
	public String toString() {
		return "Phone [brand=" + brand + ", pirce=" + pirce + "]";
	}
	public void call() {
		System.out.println("打电话");
	}
	public void takeMessage() {
		System.out.println("发短信");
	}
}

 

public class NewPhone extends Phone {

	public NewPhone() {
		super();
		// TODO Auto-generated constructor stub
	}

	public NewPhone(String brand, int pirce) {
		super(brand, pirce);
		// TODO Auto-generated constructor stub
	}
	public void playGame() {
		System.out.println("玩王者荣耀");
	}
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/860143.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号