java编译出现问题错误: 需要<标识符> Name("xx");错误: 需要<标识符> Name("xx");
publicclassAnimal//Animal{publicintage;publicvoidsetAge(intage){this.age=age;}publici...
public class Animal //Animal
{
public int age;
public void setAge(int age)
{
this.age=age;
}
public int getAge()
{
return age;
}
public boolean equals(Animal ani)
{
if(((Animal)ani).getAge()==this.age)
{
return true;
}else{
return false;
}
}
public void eat(){
System.out.println("Animal can eat!");
}
public String toString(){
return("Tis is an Animal!");
}
public static void main(String[] args)
{
//System.out.println("Hello World!");
Animal animal1=new Animal();
animal1.setAge(11);
Animal animal2=new Animal();
animal2.setAge(11);
System.out.println(animal1.equals(animal2));
Animal animal=new Animal();
System.out.println(animal);
Person per=new Person();
per.setName("ccj");
per.pet();
Student stu=new Student();
stu.teached();
}
}
class Bird extends Animal //Bird
{
}
class Dog extends Animal //Dog
{
public void shout(){
System.out.println("汪汪汪!");
}
}
class Person extends Animal //Person
{
public Dog dg=new Dog();
public String name;
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void pet(){
System.out.println("this is "+ this.name + "'s dog." );
dg.shout();
}
}
class Teacher extends Person //Teacher
{
//public Teacher(String name){
//this.name=name;
//}
}
class Student extends Person //Student
{
public Teacher tea=new Teacher();
//tea.setName("xx");
tea.setName("cc");//这边出现问题了
public void teached(){
System.out.println("I am teached by " + tea.getName());
}
} 展开
{
public int age;
public void setAge(int age)
{
this.age=age;
}
public int getAge()
{
return age;
}
public boolean equals(Animal ani)
{
if(((Animal)ani).getAge()==this.age)
{
return true;
}else{
return false;
}
}
public void eat(){
System.out.println("Animal can eat!");
}
public String toString(){
return("Tis is an Animal!");
}
public static void main(String[] args)
{
//System.out.println("Hello World!");
Animal animal1=new Animal();
animal1.setAge(11);
Animal animal2=new Animal();
animal2.setAge(11);
System.out.println(animal1.equals(animal2));
Animal animal=new Animal();
System.out.println(animal);
Person per=new Person();
per.setName("ccj");
per.pet();
Student stu=new Student();
stu.teached();
}
}
class Bird extends Animal //Bird
{
}
class Dog extends Animal //Dog
{
public void shout(){
System.out.println("汪汪汪!");
}
}
class Person extends Animal //Person
{
public Dog dg=new Dog();
public String name;
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void pet(){
System.out.println("this is "+ this.name + "'s dog." );
dg.shout();
}
}
class Teacher extends Person //Teacher
{
//public Teacher(String name){
//this.name=name;
//}
}
class Student extends Person //Student
{
public Teacher tea=new Teacher();
//tea.setName("xx");
tea.setName("cc");//这边出现问题了
public void teached(){
System.out.println("I am teached by " + tea.getName());
}
} 展开
展开全部
tea.setName("cc");//这边出现问题了
这句代码要写在方法里面,不能直接写在类中,除非你写在代码块中。
可以这样改:
{
tea.setName("cc");//这边出现问题了
}
加大括号就行了
运行结果:
true
Tis is an Animal!
this is ccj's dog.
汪汪汪!
I am teached by cc
这句代码要写在方法里面,不能直接写在类中,除非你写在代码块中。
可以这样改:
{
tea.setName("cc");//这边出现问题了
}
加大括号就行了
运行结果:
true
Tis is an Animal!
this is ccj's dog.
汪汪汪!
I am teached by cc
更多追问追答
追问
那为什么我在
class Teacher extends Person //Teacher
{
public Teacher(String name){
this.name=name;
}
}
然后:public Teacher tea=new Teacher("xx");
就可以了的啊
追答
没理解你的意思,按你说的话
但是代码:
tea.setName("cc");//这边出现问题了
还是有问题呀
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Teacher()这个方法里面根本没有setName()这个方法,能不报错么?
追问
Teacher是继承Person类的啊,Person类里有setName()啊。
追答
那你也要把方法写进来啊,如果你想在这里改变父类的值的话,你就在Teacher()类里面这么写:
class Teacher extends Person//Teacher
{
public void setName(String name)
{
this.name=name;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询