A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 耿文达 中级黑马   /  2013-5-12 18:49  /  2110 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 耿文达 于 2013-5-12 20:06 编辑

有一个类Person,具有姓名、国籍两个属性,还有一个SayHello方法,方法执行时输出“我是***我来自***”;然后定义一个Chinese类从Person类继承,现在想用继承的父类的SayHello方法调用子类的特有的姓名、国籍两个属性,而不用复写,请问怎样才能做到?我自己写了一段代码,但是只能调用父类的属性,无法调用子类的,这是为什么?请高手指教
  1. class Person
  2. {
  3. String name;
  4. String country;
  5.         
  6.         public void SayHello()
  7.         {
  8.                 System.out.println("我是"+name+','+"我来自"+country);
  9.         }
  10. }

  11. class Chinese extends Person
  12. {
  13.         String name = "张三";
  14.         String country = "中国";
  15.         }

  16. public class Test {

  17.   public static void main(String[] args)

  18.   {
  19.         Person c = new Chinese();
  20.         c.SayHello();
  21.   }
  22. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
殇_心。 + 1

查看全部评分

9 个回复

倒序浏览
可以这样理解,对于子类你只告诉了她有这个方法,你并没有告诉她这个方法是怎么样的,要怎么做。
回复 使用道具 举报
父类对象的引用无法调用子类中特有的方法和属性,这个好像只能重写了
回复 使用道具 举报
本帖最后由 蔡增辉 于 2013-5-12 19:25 编辑

你的代码有问题,你在子类中重新声明了变量name,和country,
(注意你在子类中用了 String name,String country)
这样系统认为子类中的name 和 country不是父类中的name和country,
而c.SayHello() 调用的是父类中方法,所以不能访问子类的成员变量;

评分

参与人数 1技术分 +1 收起 理由
殇_心。 + 1

查看全部评分

回复 使用道具 举报
我测试过了可以通过,你参考一下吧,但是我觉得这样的构思有点问题       希望可以帮助到你
class Person
{
        String name;
        String country;
                Person(){};
                Person(String name,String country)
            {
                        this.name=name;
                        this.country=country;
                }
        
        public void sayHello()
        {
            System.out.println("我是"+this.name+','+"我来自"+this.country);
        }
}

class Chinese extends Person
{
           static String country="Chinese";
           Chinese(){};
       Chinese(String name)
           {
                   super(name,country);

           }
}

class Test
{
        public static void main(String[] args)
        {
                        Chinese c = new Chinese("张三");
                        c.sayHello();
        }
}


评分

参与人数 1技术分 +1 收起 理由
殇_心。 + 1

查看全部评分

回复 使用道具 举报
这样吧:代码如下:
class Person
{
     String name;
     String country;
     Person(String name,String country)
     {
          this.name=name;
          this.country=country;
     }  
      public void sayHello()
     {
              System.out.println("我是"+this.name+','+"我来自"+this.country);
     }
}

class Chinese extends Person
{
        Chinese(String name,String country)
        {
                super(name,country);
        }
}

class Demo
{
        public static void main(String[] args)
        {
                Person c = new Chinese("张三","中国");
        c.sayHello();
        }
}
回复 使用道具 举报
仲伟 中级黑马 2013-5-12 20:03:18
7#
    Person(){};
                 Person(String name,String country)
             {
                         this.name=name;
                         this.country=country;
                 }

这个构造必须的,而且继承构造方法和属性
回复 使用道具 举报
这样写就用上了多态    是这样的嘛!
回复 使用道具 举报
感觉只能重写方法样
回复 使用道具 举报

  1. class Person
  2. {

  3.   private String name;
  4.    private String country;
  5. public String getName() {
  6.         return name;
  7. }

  8. public void setName(String name) {
  9.         this.name = name;
  10. }

  11. public String getCountry() {
  12.         return country;
  13. }

  14. public void setCountry(String country) {
  15.         this.country = country;
  16. }

  17.         
  18.         public void SayHello()
  19.         {
  20.                 System.out.println("我是"+name+','+"我来自"+country);
  21.         }
  22. }

  23. class Chinese extends Person
  24. {
  25.      
  26.         }
  27.        
  28.         

  29. public class Test {

  30.   public static void main(String[] args)

  31.   {
  32.         Person p = new Person();
  33.         p.setName("lisi");
  34.         p.setCountry("亚洲");
  35.         p.SayHello();
  36.         Chinese c=new Chinese();
  37.         c.setCountry("中国");
  38.         c.setName("张三");
  39.         c.SayHello();
  40.   }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马