为什么用两种不同的构造方法会导致flyable无参构造方法出错呢?
我的想法是flyable不含有name和age属性,但是带参构造为什么又可以呢?
//测试接口
public class TextFly {
public static void main(String[] args) {
Person xiaoming = new Person();
xiaoming.age=20;
xiaoming.name="xiaoming";
xiaoming.fly();
//带参构造方法
Flyable fly = new Person("xiaoli",22);
fly.fly();
//无参构造方法(报错部分,报错提示为name cannot be resolved or is not a field,age cannot be resolved or is not a field)
Flyable flyable=new Person();
flyable.name="xiaohua";
flyable.age=30;
}
//Person 类
public class Person implements Flyable{
String name;
int age;
public void eat() {
System.out.println(name+"吃东西");
}
public void sleep() {
System.out.println(name+"在睡觉");
}
public Person() {
// TODO Auto-generated constructor stub
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public void fly() {
System.out.println(name+"开始飞了");
}
}
//接口
package com.qf.Five;
public interface Flyable {
void fly() ;
}