
正文
java接口优秀代码 java接口总结
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
用JAVA编写继承与接口联合的代码是什么?
interface CanSwim {
void swim();
}
interface CanFight {
void fight();
}
interface CanFly {
void fly();
}
class ActionCharacter {
void fight() {
System.out.println("this is ActionCharacter");
}
}
class Hero extends ActionCharacter implements CanSwim, CanFly {
@Override
public void swim() {
}
@Override
public void fly() {
}
}
public class Adventure {
public static void t(CanFight x) {
x.fight();
}
public static void u(CanSwim x) {
x.swim();
}
public static void v(CanFly x) {
x.fly();
}
public static void w(ActionCharacter x) {
x.fight();
}
public static void main(String[] args) {
Hero h = new Hero();
u(h);
v(h);
w(h);
}
}
相关问答
Q1: JAVA 接口与多态 求符合下列要求的代码
多态是面向对象编程的特征之一,而接口是一系列方法的声明,是一些方法特征的集合,有特定的语法和结构,这两者根本不是同一类型和层次上的概念。接口毫无疑问可以体现出多态性来,但是多态性未必一定要用接口,只要存在方法的重写、重载与动态连接即可体现多态性(如存在继承关系的类之间),所以,不存在“什么时候用接口什么时候用多态”的问题,程序里写的具体代码只可能是接口,只是这代码可能会体现出多态性而已,两者可以在不同的概念层次上并存,不存在冲突。
简而言之,你可以在程序里用代码定义一个接口,但是你不能定义一个多态,多态只是对你代码特征的一种描述,一种概念上的抽象和总结。
Q2: 求高手帮我看段Java关于接口的一段代码
1.implements PCI是实现PCI 接口的意思;
2.不是;这两个方法是必须写的,不能少;这两个方法来自接口中的,既然现实了PCI接口,就一定要实现接口中的所有方法
3.PCI nc= new NetworkCard(); 因为NetworkCard实现了PCI接口,PCI就类似是NetworkCard的父类,这个体现了面相对象编程中的多态;就好比你是一个男生,我可以说你是一个人吧,意思是一样的;
4.PCI nc = new PCI()这个是不能直接这样写的,因为PCI是接口,接口中没有构造方法,这个是new不出来对象的
Q3: 自定义接口的JAVA代码
三个错:
1.Chicken 类里的howtoeat方法改howtoEat;
2.Apple类和Orange 放到Fruit 类外面。
3。Chicken 类的构造方法需要给重量参数,因为你只定义了一个构造方法。
我调试的没问题,改后代码:
interface Edible{
public String howtoEat();
}
class Animal{
}
class Chicken extends Animal implements Edible,Comparable{
int weight;
public Chicken(int weight){
this.weight=weight;
}
public String howtoEat(){
return "fry it";
}
public int compareTo(Object o){
return weight-((Chicken)o).weight;
}
}
class Tiger extends Animal{
}
abstract class Fruit implements Edible
{}
class Apple extends Fruit {
public String howtoEat(){
return "Make apple cider";
}
class Orange extends Fruit{
public String howtoEat(){
return "Make orange juice";
}
}
}
public class Test{
public static void main(String[] args){
Object[] objects={new Tiger(),new Chicken(10),new Apple()};
for(int i=0;iobjects.length;i++){
showObject(objects[i]);
}
}
public static void showObject(Object object){
if(object instanceof Edible)
System.out.println(((Edible)object).howtoEat());
}
}
Q4: 求JAVA接口代码
interface l1 { abstract void test(int i); } interface l2 { abstract void test(String s); } public class jiekou implements l1,l2 { public void test(int i) { System.out.println("接口1"); } public void test(String s) { System.out.println("接口2"); } public static void main(String args[]) { jiekou t=new jiekou(); t.test(42); t.test("Hello"); } } 下一个是内部接口 public class groupsix { public interface student_info { public void out(); } public class student implements student_info { int count; String name; public student(String n1) { name=n1; count++; } public void out() { System.out.println(this.name+" count="+this.count); } } public groupsix(String name1[]) { student s1; int i=0; while(iname1.length) { s1=new student(name1[i]); s1.out(); i++; } } public static void main(String args[]) { String arr[]={"A","B","C"}; groupsix g6; new groupsix(arr); } }
java接口优秀代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java接口总结、java接口优秀代码的信息别忘了在本站进行查找喔。






