In Java void method calling
In java is there a way to call a void method from a constructor.I try something like this but get an error message that the compiler can not find symbol method printThis(java.lang.String):
public class Date{
public Date(String inString){
String s = inString;
String b;
b.printThis(s);
}
public void printThis(getString)
{
System.out.printf(System.out.printf( new SimpleDateFormat("MM/dd").format开发者_如何学运维(new SimpleDateFormat("MM/dd").parse(getString) ) );
}
You want printThis(s)
- the complier is looking for a printThis method on the String instance, which does not exist.
There are LOTS of errors in the code as presented. These are the ones I spotted.
public class Date{
Problem: you are missing package declaration means this will be in the default package. That's a bad idea.
Problem: you are using a class name that is the same as commonly used classes in the standard class library. That's a bad idea.
public Date(String inString){
String s = inString;
String b;
b.printThis(s);
Error: The code attempts to invoke a method in the String API called printThis(...)
. No such method exists. You should probably get rid of b
and just call printThis(s)
Error: The code attempts to use an uninitialized local (b
) and this will give a compilation error (if you "fixed" the previous error by changing the type of b
to something that did have a printThis
method).
Problem: It is bad practice for a constructor to invoke a method on the object being constructed if there is any possibility that it might be overridden in a subclass. The problem is that the overriding method (from the subclass) might be called on the object before the superclass initialization has completed. It is safe to call static
or private
methods.
}
public void printThis(getString) {
Error: There is a syntax error in the declaration. Change getString
to String getString
.
Problem: The choice of parameter name is (IMO) nonsensical. What is a "get string"???
System.out.printf(System.out.printf(
new SimpleDateFormat("MM/dd").format(
new SimpleDateFormat("MM/dd").parse(getString) ) );
Error: Compilation error: the parentheses don't balance.
Error: Compilation error: the first argument to printf
must be a String
or a Locale
. In your code, the first argument in the outer call is a PrintStream
instance.
Error: System.out.printf(System.out.printf(
is nonsensical. You almost certainly should use just System.out.println
or System.out.print
. If you do use a printf
method you have to supply a format string in the syntax specified in the PrintStream
javadocs. (This is NOT the same as the syntax used for date formats!!!)
}
Error: missing '}' to complete class.
Problem: Your code style needs a lot of work. If you can swear on a bible that nobody else is ever going to have to read your code (!), then I suppose its OK. Otherwise, this kind of stuff is unacceptable. If this was homework, I'd dock you 50% of your marks straight off for making no attempt to get the style correct.
You have used printThis() as a method of String. If you want to print the date you might want
printThis(s);
It's not generally a good idea to use the same class name (Date) as the JDK library class
The following lines are not going to work:
String b;
b.printThis(s);
What the above code is doing is attempting to call the printThis
method on the String
object called b
.
Since a String.printThis
method does not exist, the compiler returns the error message saying that it could not find the method.
What is probably intended is the following:
printThis(s);
The above will call the printThis
method of the current instance.
You're getting that error because you're trying to call printThis() on object b, which is a String. You want:
public Date(String inString) {
printThis(inString);
}
Just FYI, it's generally inadvisable to name classes the same as JDK classes (like Date). Also the assignment you're doing of inString doesn't really achieve anything. Perhaps your code is a simplification of what you're doing but I thought I'd mention it anyway.
精彩评论