D:初始化值不同
成员变量:有默认初始化值局部变量:没有默认初始化值,必须定义,赋值,然后才能使用。注意事项:
局部变量名称可以和成员变量名称一样,在方法中使用的时候,采用的是就近原则。基本数据类型变量包括哪些:byte,short,int,long,float,double,boolean,char引用数据类型变量包括哪些:数组,类,接口,枚举A:封装概述
是指隐藏对象的属性和实现细节,仅对外提供公共访问方式。B:封装好处
隐藏实现细节,提供公共的访问方式提高了代码的复用性提高安全性。C:封装原则 将不需要对外提供的内容都隐藏起来。把属性隐藏,提供公共方法对其访问。A:学生练习
请把手机类写成一个标准类,然后创建对象测试功能。
class Demo2_Phone { public static void main(String[] args) { Phone p1 = new Phone(); p1.setBrand("三星"); p1.setPrice(5288); System.out.println(p1.getBrand() + "..." + p1.getPrice()); p1.call(); p1.sendMessage(); p1.playGame(); }}/*手机类 属性:品牌brand,价格price 行为:打电话call,发短信sendMessage,玩游戏,playGame*/class Phone { //java bean private String brand; //品牌 private int price; //价格 public void setBrand(String brand) { //设置品牌 this.brand = brand; } public String getBrand() { //获取品牌 return this.brand; //this.可以省略,你不加系统会默认给你加 } public void setPrice(int price) { //设置价格 this.price = price; } public int getPrice() { //获取价格 return price; } public void call() { //打电话 System.out.println("打电话"); } public void sendMessage() { //发短信 System.out.println("发短信"); } public void playGame() { //玩游戏 System.out.println("玩游戏"); }}新闻热点
疑难解答