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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© HM刘俊 高级黑马   /  2013-3-10 10:54  /  1772 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

以下这段程序为什么运行不了。


class Test5<S>{     //创建泛型类的实例。
   private S s;
    public S getS(){
     return s;
      }
     public void setS(S s){
    this.s=s;
     }
    public static void main(String[] args){
     
     Test<String> test1=new Test<String>();
     test1.setS("abc");
     System.out.println(test1.getS());
     Test<Integer> test2=new Test<Integer>();
     test2.setS(new Integer(1));
     System.out.println(test2.getS());
    }
}

5 个回复

倒序浏览
本帖最后由 袁见 于 2013-3-10 11:19 编辑

public class Test5<S> { // 创建泛型类的实例。
        private S s;

        public S getS() {
                return s;
        }

        public void setS(S s) {
                this.s = s;
        }

        public static void main(String[] args) {

                Test5<String> test1 = new Test5<String>();//这个地方应Test5而不是Test
                test1.setS("abc");
                System.out.println(test1.getS());
                Test5<Integer> test2 = new Test5<Integer>();//同样的这个地方应Test5而不是Test
                test2.setS(new Integer(1));
                System.out.println(test2.getS());
        }
}

希望你在一个的编码中细心一点。
回复 使用道具 举报
你创建对象写错了,上面定义的类名是Test5,而下面创建的对象的用的类名是Test,就这个小错误,细心点,运行结果应该是abc和1.
回复 使用道具 举报
  1. class Test<S>{     //创建泛型类的实例。
  2.         private S s;
  3.        
  4.         public S getS(){
  5.                 return s;
  6.         }
  7.        
  8.         public void setS(S s){
  9.                 this.s=s;
  10.         }  
  11. }

  12. public class Qu{
  13.         public static void main(String[] args){
  14.      
  15.          Test<String> test1=new Test<String>();
  16.          test1.setS("abc");
  17.          System.out.println(test1.getS());
  18.          
  19.          Test<Integer> test2=new Test<Integer>();
  20.          test2.setS(new Integer(1));
  21.          System.out.println(test2.getS());
  22.         }
  23. }
复制代码
是要这个么?
回复 使用道具 举报
你的代码有问题啊 修改后如下所示 就可以运行了
回复 使用道具 举报
谢谢大家,我知道了。太粗心了,以后写代码会细心点的。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马