要注意的几个点:
1.内部类的话是指属性以及this关键字都写进去。
2.因为主函数是静态,因此上面的函数也要加上static
参考实例如下:
public class Test12 {
//用内部类的方法
//私有化属性后再构造方法
private static class Phone{
private String brand;
private String desc;
private int count;
private String store;
public Phone(String brand,String desc,int count,String store){
this.brand=brand;
this.desc=desc;
this.count=count;
this.store=store;
}
public void show(){
System.out.println("品牌 :"+brand
+"\n商品介绍 :"+desc+
"\n评论数 :"+count+
"\n商家信息 :"+store);
}
}
public static void main(String[] args) {
System.out.println("------手机信息-------");
Phone phone=new Phone("小米","小米13 徕卡光学镜头 第二代骁龙8处理器",
100000,"小米京东自营旗舰店");
phone.show();
}
}