本帖最后由 耿渊博 于 2014-3-21 21:55 编辑
在Java中虽然不支持多重继承,但是Java提供的接口可以支持多重继承,那么以下代码能否模拟多重继承?- package com.itheima;
- //定义IPeople接口
- interface IPeople{
- String getName();
- void setName(String name);
- String getSex();
- void setSex(String sex);
- }
- //定义ITeacher
- interface ITeacher extends IPeople{
- String teach();
- }
- //定义IStudent
- interface IStudent extends IPeople{
- String study();
- }
- public class Person implements IPeople,ITeacher,IStudent{
- String name="";
- String sex="";
- public String getName(){
- return name;
- }
- public void setName(String name){
- this.name = name;
- }
- public String getSex(){
- return sex;
- }
- public void setSex(String sex){
- this.sex = sex;
- }
- public String teach(){
- return "姓名: "+name+""+sex+"教师";
- }
- public String study(){
- return "姓名: "+name+""+sex+"学生";
- }
- public static void main(String[] args){
- Person p = new Person();
- p.setName("张三 ");
- p.setSex("男 ");
- System.out.println(p.teach());
- p.setName("李四 ");
- p.setSex("女 ");
- System.out.println(p.study());
- }
- }
复制代码
|