java第九次学习
static关键字
class Person {
//姓名
String name;
//年龄
int age;
//国籍
//String country;
static String country;
public Person(){}
public Person(String name,int age) {
this.name = name;
this.age = age;
}
public Person(String name,int age,String country) {
this.name = name;
this.age = age;
this.country = country;
}
public void show() {
System.out.println("姓名:"+name+",年龄:"+age+",国籍:"+country);
}
}
class PersonDemo {
public static void main(String[] args) {
//创建对象1
Person p1 = new Person("张三",16,"中国");
p1.show();
//创建对象2
//Person p2 = new Person("李四",22,"中国");
//p2.show();
Person p2 = new Person("李四",22);
p2.show();
//创建对象3
//Person p3 = new Person("王五",20,"中国");
//p3.show();
Person p3 = new Person("王五",20);
p3.show();
p3.country = "美国";
p3.show();
p1.show();
p2.show();
}
}
代码块
java中用{}起来的称为代码块,分为局部代码块,构造代码块,静态代码块,同步代码块。
静态变量和成员变量区别
继承
消除重复
提高软件的复用性,维护性,但是也带来耦合性很强的弊端。
设计原则
高内聚低耦合
java中只支持单继承,不支持多继承