【问题标题】:What is a relationship between System class and PrintStream ClassSystem类和PrintStream类有什么关系
【发布时间】:2013-10-22 06:38:29
【问题描述】:

我想知道 System.out.println() 到底是什么。看了这两篇文章 What is System, out, println in System.out.println() in JavaWhat's the meaning of System.out.println in Java?。我知道什么是 System,out,and print,但我不知道 System 类如何连接到PrintStream class。他们怎么样related to each other

System是java.lang package.out中的一个类,是System类的静态成员,那么它是如何变成java.io.PrintStream的实例的呢?System和PrintStream是如何相互关联的呢?

【问题讨论】:

标签: java system.out


【解决方案1】:

System类和PrintStream类的关系是HAS-A关系。 这里 System 类 HAS-A PrintStream 类。 理解关系理解程序。

class A
{    
    void display()
    {   
        System.out.pritln("this is display method");
    }

}

class B

{
    static A ob=new A();
}

class demo
{
    public static void main()
    {
    B.ob.display();
    }
}

它打印出这是显示方法。

B.ob.display() 就像 System.out.println()。

在 B 类中创建一个对象。

在 System 类中创建 PrintStream 类对象。

ob 是 A 类的静态对象引用。

还有 PrintStream 类的静态引用。

【讨论】:

    【解决方案2】:

    System类有PrintStream类的静态对象,在System类中声明为outprintln()PrintStream类的方法。

    所以我们可以以System.out 访问静态对象,而println()PrintStream 类的方法。这就是为什么我们可以写System.out.println() 以及这两个类是如何相关的。

    【讨论】:

      猜你喜欢
      • 2011-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-18
      • 1970-01-01
      • 1970-01-01
      • 2011-08-23
      相关资源
      最近更新 更多