类作为成员变量类型使用:
自定义的类也可以作为成员变量使用,如:
// 1.创建一个电脑角色的类:
public class Computer{private String computername;//定义电脑名称,String类型底层实际就是一个类,是java作者编写的类,因此我们可以自己编写类给成员变量使用。// 3.使用Mouse类作为Computer类的成员变量:private Mouse mousename;//电脑配备的鼠标// 6.定义一个方法测试Mouse类型(class作为成员变量类型)作为成员变量使用:public void infortest(){System.out.println(computername + "电脑配备了"+ mousename.getMousename() + "正在搬砖");//灵越电脑配备了双飞燕鼠标正在搬砖};public Computer(){};//无参构造public Computer(String computername,Mouse mousename){//全参构造this.computername = computername;this.mousename = mousename;};public String getComputername(){return computername;};public void setComputername(String computername){this.computername = computername;};public Mouse getMousename(){return mousename;};public void setMousename(Mouse mousename){this.mousename = mousename;};
}
// 2.定义一个鼠标的类:
public class Mouse{private String mousename;//鼠标名称public Mouse(){};//无参构造public Mouse(String mousename){//有参构造this.mousename = mousename;};public String getMousename(){//get-方法return mousename;};public void setMousename(String mousename){//set-方法this.mousename = mousename;};
};
// 4.创建一个用来实现Computer类的实例对象:
public class Computerobj{public static void main(String[] args){// 创建一个电脑对象:Computer computer = new Computer();computer.setComputername("灵越");// 创建一个鼠标对象:Mouse mouse = new Mouse("双飞燕鼠标");// 5.为电脑配备鼠标:computer.setMousename(mouse);// 7.调用computer的测试方法infortest:computer.infortest();//灵越电脑配备了双飞燕鼠标正在搬砖};
};
接口作为成员变量类型使用:
类可以作为成员变量类型使用,接口也是可以作为成员变量使用的,如:
// 1.定义一个电脑类:
public class Computer{private String computername;//电脑名称private Keyboard keyboardname;//键盘名称// 5.进行测试:public void useKeybord(){System.out.print(computername+"电脑配备了");keyboardname.use();//调用接口中的抽象方法输出了:苹果电脑配备了双飞燕键盘,正在进行搬砖};public Computer(){};public Computer(String computername,Keyboard keyboardname){this.computername = computername;this.keyboardname = keyboardname;};public String getComputername(){return computername;};public void setComputername(String computername){this.computername = computername;};public Keyboard getKeyboardname(){return keyboardname;};public void setKeyboardname(Keyboard keyboardname){this.keyboardname = keyboardname;};
};
// 2.定义一个键盘接口:
public interface Keyboard{void use();
};
// 3.定义一个接口的实现类Keyboardimpclass:
public class Keyboardimpclass implements Keyboard{@Overridepublic void use(){System.out.println("双飞燕键盘,正在进行搬砖");};
};
// 4.创建有一个Computer的对象:
public class ComputerObjTest{public static void main(String[] args){Computer computer = new Computer();computer.setComputername("苹果");//设置电脑名称computer.setKeyboardname(new Keyboardimpclass());//给电脑配备键盘,这里创建了实现类对象,也可以使用匿名对象类,如:/* Keyboard keyboard = new Keyboard(){@Overridepublic void use(){System.out.println("双飞燕键盘正在被用来搬砖");};};computer.setKeyboardname(keyboard); *//* 除上面外,还可以直接传入一个接口,如:computer.setKeyboardname(new Keyboard(){@Overridepublic void use(){System.out.println("双飞燕键盘正在被用来搬砖");};});*/computer.useKeybord();//电脑使用键盘};
};
接口作为方法的参数或返回值使用:
import java.util.ArrayList;
import java.util.List;
public class Listinterface{public static void main(String[] args){List<String> list = new ArrayList();List<String> result = addNames(list);for(int i = 0;i <= result.size();i++){System.out.println(result.get(i));}};// 定义了一个既有参数又有返回值的方法:public static List<String> addNames(List<String>list){list.add("成员1");list.add("成员2");list.add("成员3");return list;}
}
提示:本文图片等素材来源于网络,若有侵权,请发邮件至邮箱:810665436@qq.com联系笔者删除。
笔者:苦海