接口

school 接口

public interface school {

void setSchoolName(String schoolName);

void setClassName(String className);

void setSex(String Sex);

String getSchoolName();

String getClassName();

String getSex();

}

student 实现接口的类

public class student implements school {

private String schoolName;

private String className;

private String Sex;

public String getSchoolName() {

return schoolName;

}

public void setSchoolName(String schoolName) {

this.schoolName = schoolName;

}

public String getClassName() {

return className;

}

public void setClassName(String className) {

this.className = className;

}

public String getSex() {

return Sex;

}

public void setSex(String sex) {

this.Sex = sex;

}

public String toString() {

String str = "学校名称" + schoolName +"班级名称" + className + "性别"+Sex;

return str;

}

public static void main(String[] args) {

student stu = new student();

stu.setSchoolName("济宁示范");

stu.setSex("男");

stu.setClassName("二班");

System.out.println(stu.toString());

}

}

接口

1.接口可以多继承

2.接口的方法声明必须是 public abstract 即便不写默认也是

3.接口里面不能包含方法具体实现

4.类实继承接口必须实现接口里申明的全部方法,除非该类是抽象类

5.类里面可以声明 public static final 修饰的变量

6.接口不能被实例化,但是可以被实现类创建

你可能感兴趣的:(接口)