[故事有你] 2015-03-25 13:01 采纳率: 100%
浏览 1599

第一次学java,有一些不懂地方。请各位帮我解答一下。

看视频看到多态性那边了。但是对对象的向上转型和向下转型的概念还是模糊不清的,有没有好心人给我解释一下。
还有一个问题:
比如有一段代码如下:

class Info{ // 定义信息类
    private String name = "ABCD";    // 定义name属性
    private String content = "JAVA讲师"  ;        // 定义content属性
    private boolean flag = false ;  // 设置标志位
    public synchronized void set(String name,String content){
        if(!flag){
            try{
                super.wait() ;
            }catch(InterruptedException e){
                e.printStackTrace() ;
            }
        }
        this.setName(name) ;    // 设置名称
        try{
            Thread.sleep(300) ;
        }catch(InterruptedException e){
            e.printStackTrace() ;
        }
        this.setContent(content) ;  // 设置内容
        flag  = false ; // 改变标志位,表示可以取走
        super.notify() ;
    }
    public synchronized void get(){
        if(flag){
            try{
                super.wait() ;
            }catch(InterruptedException e){
                e.printStackTrace() ;
            }
        }
        try{
            Thread.sleep(300) ;
        }catch(InterruptedException e){
            e.printStackTrace() ;
        }
        System.out.println(this.getName() + 
            " --> " + this.getContent()) ;
        flag  = true ;  // 改变标志位,表示可以生产
        super.notify() ;
    }
    public void setName(String name){
        this.name = name ;
    }
    public void setContent(String content){
        this.content = content ;
    }
    public String getName(){
        return this.name ;
    }
    public String getContent(){
        return this.content ;
    }
};
class Producer implements Runnable{ // 通过Runnable实现多线程
    private Info info = null ;      // 保存Info引用
    public Producer(Info info){
        this.info = info ;
    }
    public void run(){
        boolean flag = false ;  // 定义标记位
        for(int i=0;i<50;i++){
            if(flag){
                this.info.set("ABCD","JAVA讲师") ;    // 设置名称
                flag = false ;
            }else{
                this.info.set("mldn","www.mldnjava.cn") ;   // 设置名称
                flag = true ;
            }
        }
    }
};
class Consumer implements Runnable{
    private Info info = null ;
    public Consumer(Info info){
        this.info = info ;
    }
    public void run(){
        for(int i=0;i<50;i++){
            this.info.get() ;
        }
    }
};
public class ThreadCaseDemo03{
    public static void main(String args[]){
        Info info = new Info(); // 实例化Info对象
        Producer pro = new Producer(info) ; // 生产者
        Consumer con = new Consumer(info) ; // 消费者
        new Thread(pro).start() ;
        new Thread(con).start() ;
    }
};

不懂的地方是: private Info info = null ;

变量info的类型使用了类Info,为什么要这么用呢?这样做的作用是什么?

  • 写回答

5条回答 默认 最新

  • threenewbee 2015-03-25 13:09
    关注

    这很正常,好比
    String string = "a";
    为什么string是String类型呢?只要不是关键字,你完全可以这么定义,Java区分大小写。

    评论

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)