简体   繁体   English

在java中替换system.out.println(class)

[英]replacing system.out.println(class) in java

I've got some classes which include toString functions which work very well as: 我有一些包含toString函数的类,它们可以很好地工作:

snipped of declaration: 剪下声明:

public String toString() {
        return "Port info: "+myPort.variable;
    }

snipped of main(): 剪切了main():

Port myPort;
myPort.fillInfo();
system.out.println(myPort);

output: 输出:

"Port info: 123"

I'm trying to replace all my system.out.println(myPort) calls with myWindow.println(myPort) calls where myWindow contains: 我正在尝试使用MyWindow包含的myWindow.println(myPort)调用替换所有的system.out.println(myPort)调用:

public void println(String toPrint)
{
    textArea.append(toPrint+"\n"); //or insert
}

However, I'm getting: 但是,我得到了:

The method println(String) in the type window is not applicable for the arguments (Port)

In other words, my declaration is expecting the String type, and I'm trying to pass it the Port type. 换句话说,我的声明期待String类型,我试图将它传递给Port类型。

Somehow, system.out.println() will take any class that's got a toString() declared. 不知何故,system.out.println()将获取任何声明了toString()的类。

How does system.out.println take any class and print it out, and run its toString() method if it exists? system.out.println如何获取任何类并将其打印出来,并运行其toString()方法(如果存在)? And, more to the point, how can I replicate this functionality for my own println() function? 而且,更重要的是,我如何为我自己的println()函数复制此功能?

Change your Window to Window更改为

public void println(Object obj)
{
    textArea.append(obj +"\n"); 
}

PrintStream.println has an overload that takes an Object argument. PrintStream.println有一个带有Object参数的重载。 You can make your println do the same. 你可以让你的println做同样的事情。

First, please don't use \\n as a line separator (it isn't portable). 首先,请不要使用\\n作为行分隔符(它不可移植)。 In addition to overloading println(Object) , you could make your method generic. 除了重载println(Object) ,您还可以使方法具有通用性。 Something like 就像是

public <T> void println(T obj)
{
    textArea.append(String.format("%s%n", obj)); 
}

or 要么

public void println(Object obj)
{
    textArea.append(String.format("%s%n", obj)); 
}

or 要么

public void println(Object obj)
{
    textArea.append((obj == null ? "null" : obj.toString()) 
            + System.lineSeparator()); 
}

The problem is that System.out has a method to print to console an object as it contains for all primitive data types. 问题是System.out有一个方法来打印控制对象,因为它包含所有原始数据类型。 The thing about this is that as all methods have the same name and just change the data type of the parameter you want to print, you think you can pass an object by a string and is not. 关于这一点的是,因为所有方法都具有相同的名称,只是更改要打印的参数的数据类型,您认为可以通过字符串传递对象而不是。 The method .println() automatically takes which passes an object. 方法.println()自动获取传递对象的方法。 Within this method .println() he takes the object that you indicated by parameters and calls his method .toString() to obtain the string representation of the object and printed on the console. 在此方法.println()中,他获取您通过参数指示的对象并调用其方法.toString()以获取对象的字符串表示形式并打印在控制台上。

If you want to print any type of object you must declare your parameter as object type and invoke the method .toString() from the object and print that information. 如果要打印任何类型的对象,必须将参数声明为对象类型,并从对象调用方法.toString()并打印该信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM