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

建立TestInstance 类,在类中定义方法method1(Person e);
在method1中:
(1)根据e的类型调用相应类的getInfo()方法。
(2)根据e的类型执行:
如果e为Person类的对象,输出:“a person”;
如果e为Student类的对象,输出
“a student”
“a person ”
如果e为Graduate类的对象,输出:
“a graduated student”
“a student”
“a person”
  1. package com.ex4.duotai;

  2. public class TestInstance {

  3.         /**
  4.          * @param args
  5.          */
  6.         public static void main(String[] args) {
  7.                 TestInstance ti=new TestInstance();
  8.                 Person p1=new Person();//这里报错。。。怎么回事?
  9.                 Student student=new Student();
  10.                 Graduate gra=new Graduate();
  11.                
  12.                 ti.method1(p1);
  13.                 System.out.println();
  14.                
  15.                 ti.method1(student);               
  16.                 System.out.println();
  17.                
  18.                 ti.method1(gra);
  19.                 System.out.println();
  20.                
  21. ;               
  22.                
  23.                
  24.      
  25.      
  26.         }
  27.         public void method1(Person e){
  28.                 String info=e.getInfo();
  29.                 System.out.println(info);
  30.                
  31.         }
  32.         class Person {
  33.                 protected String name="person";
  34.                 protected int age=50;
  35.                 public String getInfo() {
  36.                           return "Name: "+ name + "\n" +"age: "+ age;
  37.                 }
  38.         }
  39.         class Student extends Person {
  40.                 protected String school="pku";
  41.                 public String getInfo() {
  42.                             return  "Name: "+ name + "\nage: "+ age
  43.                           + "\nschool: "+ school;
  44.                 }
  45.                
  46.         }
  47.         class Graduate extends Student{
  48.                 public String major="IT";
  49.                 public String getInfo()
  50.                 {
  51.                         return  "Name: "+ name + "\nage: "+ age
  52.                           + "\nschool: "+ school+"\nmajor:"+major;
  53.                 }
  54.         }


  55. }
复制代码
问题怎么解决啊




评分

参与人数 1技术分 +1 黑马币 +6 收起 理由
枫儿 + 1 + 6 神马都是浮云

查看全部评分

9 个回复

倒序浏览
楼主应该大意了,把Preson类、Student类和Graduate类定义到了TestInstance类里面了
应该把这三个类写到TestInstance类外面,代码如下:
  1. public class TestInstance {
  2.         /**
  3.          * @param args
  4.          */
  5.         public static void main(String[] args) {
  6.                 TestInstance ti=new TestInstance();
  7.             Person p1=new Person();//这里报错。。。怎么回事?
  8.             Student student=new Student();
  9.             Graduate gra=new Graduate();
  10.             
  11.             ti.method1(p1);
  12.             System.out.println();
  13.             
  14.             ti.method1(student);               
  15.             System.out.println();
  16.             
  17.             ti.method1(gra);
  18.             System.out.println();
  19.         }
  20.         public void method1(Person e){
  21.                 String info=e.getInfo();
  22.             System.out.println(info);
  23.         }
  24. }
  25. class Person {
  26.     protected String name="person";
  27.     protected int age=50;
  28.     public String getInfo() {
  29.             return "Name: "+ name + "\n" +"age: "+ age;
  30.     }
  31. }
  32. class Student extends Person {
  33.     protected String school="pku";
  34.     public String getInfo() {
  35.             return  "Name: "+ name + "\nage: "+ age + "\nschool: "+ school;
  36.     }
  37.    
  38. }
  39. class Graduate extends Student{
  40.     public String major="IT";
  41.     public String getInfo(){
  42.             return  "Name: "+ name + "\nage: "+ age + "\nschool: "+ school+"\nmajor:"+major;
  43.     }
  44. }
复制代码

评分

参与人数 1技术分 +1 黑马币 +6 收起 理由
枫儿 + 1 + 6 群里零度姐说的李文帅? 加油.

查看全部评分

回复 使用道具 举报
.Mr 中级黑马 2013-11-30 14:49:39
藤椅
你这个是错误地把Person类定义在了TestInstance中,使得Person变成了TestInstance的内部类了,应该将Person定义在Testinstance的外面。其他的Stunden,Gduate有同样的问题。
回复 使用道具 举报
李文帅 发表于 2013-11-30 14:49
楼主应该大意了,把Preson类、Student类和Graduate类定义到了TestInstance类里面了
应该把这三个类写到Test ...

........现在该对了。。。。下次得注意
回复 使用道具 举报
.Mr 发表于 2013-11-30 14:49
你这个是错误地把Person类定义在了TestInstance中,使得Person变成了TestInstance的内部类了,应该将Person ...

好的。。。。。。。
回复 使用道具 举报
内部类定义在成员位置上,是可以用成员修饰符修饰的,main是静态方法,只能调用类内部的静态成员,所以这三个类都加上static声明静态就可以了
回复 使用道具 举报
本帖最后由 25343215 于 2013-11-30 19:44 编辑

楼主出现的问题是因为:
要创建内部类的对象,需要通过 new 外部类().new 内部类()的方法,才能创建。

当然直接在类外定义这类,更符合楼主的需求。

  1. public class TestInstance {

  2.         /**
  3.          * @param args
  4.          */
  5.         public static void main(String[] args) {
  6.                 TestInstance ti=new TestInstance();
  7.                 Person p1=new TestInstance().new Person();//这里报错。。。怎么回事?
  8.                 Student student=new TestInstance().new Student();
  9.                 Graduate gra=new TestInstance().new Graduate();
  10.                
  11.                 ti.method1(p1);
  12.                 System.out.println();
  13.                
  14.                 ti.method1(student);               
  15.                 System.out.println();
  16.                
  17.                 ti.method1(gra);
  18.                 System.out.println();
  19.                
  20. ;               
  21.                
  22.                
  23.      
  24.      
  25.         }
  26.         public void method1(Person e){
  27.                 String info=e.getInfo();
  28.                 System.out.println(info);
  29.                
  30.         }
  31.         class Person {
  32.                 protected String name="person";
  33.                 protected int age=50;
  34.                 public String getInfo() {
  35.                           return "Name: "+ name + "\n" +"age: "+ age;
  36.                 }
  37.         }
  38.         class Student extends Person {
  39.                 protected String school="pku";
  40.                 public String getInfo() {
  41.                             return  "Name: "+ name + "\nage: "+ age
  42.                           + "\nschool: "+ school;
  43.                 }
  44.                
  45.         }
  46.         class Graduate extends Student{
  47.                 public String major="IT";
  48.                 public String getInfo()
  49.                 {
  50.                         return  "Name: "+ name + "\nage: "+ age
  51.                           + "\nschool: "+ school+"\nmajor:"+major;
  52.                 }
  53.         }


  54. }
复制代码

评分

参与人数 1技术分 +1 黑马币 +6 收起 理由
枫儿 + 1 + 6 赞一个!

查看全部评分

回复 使用道具 举报
冷月 高级黑马 2013-12-1 12:23:00
8#
楼主,代码修改过了,希望对你有帮助
  1. //package com.ex4.duotai;

  2. public class TestInstance {

  3.         /**
  4.          * @param args
  5.          */
  6.         public static void main(String[] args) {
  7.                 TestInstance ti=new TestInstance();
  8.                 Person p1=new Person();
  9.                 Student student=new Student();
  10.                 Graduate gra=new Graduate();
  11.                
  12.                 ti.method1(p1);
  13.                 System.out.println();
  14.                
  15.                 ti.method1(student);               
  16.                 System.out.println();
  17.                
  18.                 ti.method1(gra);
  19.                 System.out.println();
  20.                     
  21.         }
  22.         public void method1(Person e){
  23.                 String info=e.getInfo();
  24.                 System.out.println(info);
  25.                
  26.         }
  27. }
  28. class Person {
  29.                 protected String name="person";
  30.                 protected int age=50;
  31.                 public String getInfo() {
  32.                           return "Name: "+ name + "\n" +"age: "+ age;
  33.                 }
  34.         }
  35.         class Student extends Person {
  36.                 protected String school="pku";
  37.                 public String getInfo() {
  38.                             return  "Name: "+ name + "\nage: "+ age
  39.                           + "\nschool: "+ school;
  40.                 }
  41.                
  42.         }
  43.         class Graduate extends Student{
  44.                 public String major="IT";
  45.                 public String getInfo()
  46.                 {
  47.                         return  "Name: "+ name + "\nage: "+ age
  48.                           + "\nschool: "+ school+"\nmajor:"+major;
  49.                 }
  50.         }
复制代码
回复 使用道具 举报
25343215 发表于 2013-11-30 19:06
楼主出现的问题是因为:
要创建内部类的对象,需要通过 new 外部类().new 内部类()的方法,才能创建。

                                 嗯
回复 使用道具 举报
冷月 发表于 2013-12-1 12:23
楼主,代码修改过了,希望对你有帮助

                        嗯
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马