获取类名

huangapple go评论92阅读模式
英文:

Getting class name

问题

我正在编写一个管理员类,我编写的所有其他类都打算从这个类继承。在这种情况下,我希望这些类继承一个主方法,并计划实现一个工厂。有一些sysout我必须编写,其中包括诸如charcount、linecount等各种类名。为此,我需要能够获取当前的类名,因为这个方法将被多个类继承。在Java中是否有一种方法可以实现这一点?您可以在下面找到我想要进行的sysout的示例:

System.out.println("Usage: className <src>\n"); 

非常感谢您提前的帮助,我非常感激!

编辑:
大家好!

非常抱歉之前我解释得不清楚,我将在下面展示整个代码并尝试进一步解释我的想法。所以,我要做的事情本质上是制作一个主方法,检查文件是否存在,如果文件存在,它将尝试执行某个特定的函数(所有这些都将在命令行中进行)。我正在练习工厂设计模式(这是我以前从未做过的事情)。我将使用主方法的执行部分来调用某个工厂(尚未制作)来创建管理员子类的对象,并返回文件中的charcount和文件中的linecount等内容。上面的问题涉及到主方法的一部分,该部分将打印子类的用法。所以例如,我将从管理员继承charcount,因为检查文件并在管理员的主方法中进行,但是当我打印用法消息时,我不希望它打印“Usage: Administrator ”,而是打印“Usage: charcount ”。我希望这能解决问题,并且再次为我之前的糟糕解释感到抱歉,您可以在下面的代码中找到我目前为止的代码:

public class Administrator 
{
	
	private static File srcFile = null; 
	private static String srcFilename = "<srcFilename>"; 
	
	
	public static void main(String[] args) throws IOException
	{    
		 
		
		// 检查命令行 
		if(args.length != 1) 
		{ 
			System.out.println("Usage: charcount <src>\n"); 
			return;
		} 
		
		// 检查参数是否有效,检查srcFile是否存在,以及是否可以创建dstFile。
		if (args[0] != null)
		{ 
			// 检查 <src> 
			srcFilename = args[0]; 
			System.out.println(": srcFilename '" + srcFilename + "'"); 
			srcFile = new File(srcFilename); 
			if(!srcFile.canRead()) 
			{ 
				System.out.println("charcount: 无法打开 srcFile '" + srcFilename + "'"); 
				return;
			}
		} 
		else 
		{ 
			System.out.println("charcount: [OK] srcFilename = '" + srcFilename + "'"); 
			
		}  
	} 
	
	public static int execute() {return 0;}
	
}
英文:

I am writing an administrator class that all other classes I am writing are meant to inherit from. In this case I want the classes to inherit a main method, and I am planning on implementing a factory as well. There are a few sysouts that I have to write that include various class names such as charcount, linecount and the such. For this I need to be able to get the current class name, since this method will be inherited by multiple classes. Is there a way to do this in java? You can find an example of the sysout I am looking to do below:

System.out.println(&quot;Usage: className &lt;src&gt;\n&quot;); 

Thank you all in advance, any help is appreciated!

edit:
Hey guys!

Very sorry I wasnt clear before, I'll show the entire code below and try to further explain my idea. So what I am doing is essentially making a main method that checks to see if a file exists, and if the file exists, it will try to execute a certain function (All this will be in the command line).I am practicing factory design patterns (something I've never done before). I will use the execute part of the main method to call on some factory (not yet made) to create objects of subclasses of administrator and return things like charcount in a file and linecount in a file. The above question refers to one part of this main method that will print the usage of the subclass. So for example, I will have charcount inherit from administrator because the process of checking the file and is in the main method of administrator, however when I print the usage message, I dont want it to print "Usage: Administrator <src>", but "Usage: charcount <src>". I hope that clears things up, and sorry again for how badly I explained it above, you can find what I have so far as code below:

public class Administrator 
{
	
	private static File srcFile = null; 
	private static String srcFilename = &quot;&lt;srcFilename&gt;&quot;; 
	
	
	public static void main(String[] args) throws IOException
	{    
		 
		
		//check the command line 
		if(args.length != 1) 
		{ 
			System.out.println(&quot;Usage: charcount &lt;src&gt;\n&quot;); 
			return;
		} 
		
		// Check if arguments are valid, if the srcFile exists, and if can create the dstFile.
		if (args[0] != null)
		{ 
			//check &lt;src&gt; 
			srcFilename = args[0]; 
			System.out.println( &quot;: srcFilename &#39;&quot; + srcFilename + &quot;&#39;&quot;); 
			srcFile = new File(srcFilename); 
			if(!srcFile.canRead()) 
			{ 
				System.out.println(&quot;charcount: cannot open srcFile &#39;&quot; + srcFilename + &quot;&#39;&quot;); 
				return;
			}
		} 
		else 
		{ 
			System.out.println(&quot;charcount: [OK] srcFilename = &#39;&quot; + srcFilename + &quot;&#39;&quot;); 
			
		}  
	} 
	
	public static int execute() {return 0;}
	
}

答案1

得分: 1

你可以这样做:

System.out.println("类名:" + getClass().getName());

这将返回 foo.bar.Baz(假设您的类名为 Baz,位于包 foo.bar 中)。

或者你可以这样做:

System.out.println("类名:" + getClass().getSimpleName());

这将简单地返回 'Foo'。

您可能会发现这个相关的问题有帮助:Java 中规范名称、简单名称和类名之间的区别是什么?

编辑:问题中询问从“main”方法中进行此操作,这可能被理解为 public static void main(String[] args),或者作为给定类层次结构中的主要方法。由于问题接着讨论该方法被多个类继承,我将“main”理解为“主要”。这个答案在从 public static void main(String[] args) 中不起作用,因为 getClass() 是实例方法,无法从静态上下文中调用。

英文:

You can do something like this:

System.out.println(&quot;Class name: &quot; + getClass().getName());

Which will return foo.bar.Baz (assuming your class is named Baz and in package foo.bar)

Or you can
System.out.println(&quot;Class name: &quot; + getClass().getSimpleName());

Which will simply return 'Foo'

You might find this related question helpful: What is the difference between canonical name, simple name and class name in Java Class?

Edit: The question asks about doing this from the "main" method, which could be understood as public static void main(String[] args) or as the primary method in a given class hierarchy. Since the question goes on to discuss the method being inherited by multiple classes, I understood "main" as "primary". This answer wouldn't work from public static void main(String[] args) because getClass() is an instance method and can't be invoked from a static context.

答案2

得分: 0

Java的Class类具有一些有用的方法。您可以在运行时使用getClass().getSimpleName()。此方法根据对象的类型调用。

英文:

Java Class class has some useful methods. You can use getClass().getSimpleName(). at runtime this method called based on object's type.

答案3

得分: 0

尝试使用以下方法:

this.getClass().getCanonicalName() 或者 this.getClass().getSimpleName()

如果它是匿名类,您应该使用 this.getClass().getSuperclass().getName()

英文:

Try using this methods:

this.getClass().getCanonicalName() or this.getClass().getSimpleName().

If it's an anonymous class, you should use this.getClass().getSuperclass().getName()

huangapple
  • 本文由 发表于 2020年9月28日 21:13:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64102923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定