可是如果是public class a { public string b (int ab) {.........} int ab = 9; b(ab);//能这样调用吗?如果可以的话~那这个方法的返回类型在这个调用中并没有能体现啊? }
這樣當然不行啦~~~ public class a { public static String b(int ab) { return "hello" + ab; } public static void main(String[] args) { int ab = 9; System.out.println(b(ab)); } }
这位仁兄,你好像错了。你定义的方法,必须要在其他的方法里边调用,直接放在类里边是不成的。或者你把方法b定义成静态的,然后在static块里边可以调用。public class a{ public void b(){ …… } public void c(){ a ac=new a(); a.b(); } }或者public class a{ static{ b(); } public static void b(){ } }
class A { public static void staticMethod() { System.out.println("staticMethod"); } public void instanceMethod() { System.out.println("instanceMethod"); } public static void staticMethod2() { staticMethod(); } } public class B { public static void main(String[] args) { String sm = A.staticMethod();//钡秸ノclass A public static method A a = new A(); String im = a.instanceMethod();//ネΘclass A 龟ㄒ秸ノclass A 龟ㄒよ猭 System.out.println("sm: " + sm); System.out.println("im: " + im); } }
这位仁兄,你好像错了。 我的method b是靜態的,呵呵
to : haode(好的) public class a{ public void b(){ …… } public void c(){ b(); } } 直接调用b()就可以了吧 非静态方法里可以调用同一个类里的静态方法和非静态方法 静态方法里只能直接调用静态方法了
thinking in java 講的很詳細,做編些小程序。 如果真想把Java的語法學透,看看Scjp的練習,很多平時用不到的語法~~最好別看,呵呵
我想你說的大概是這個意思: class A { public void method() { System.out.println("A method"); } } class B extends A{ public void method() { System.out.println("B method"); }
} public Test { public static void main(String[] args) { B b = new B(); b.method(); //print B method A a = new A(); a.method(); //print A method A a1 = new B(); a1.method(); //print B method } } 如果 method都是靜態方法, A a1 = new B(); a1.method(); 輸出結果會不同~~
{
public string b (int ab)
{.........} int ab = 9;
b(ab);//能这样调用吗?如果可以的话~那这个方法的返回类型在这个调用中并没有能体现啊?
}
public class a {
public static String b(int ab) {
return "hello" + ab;
}
public static void main(String[] args) {
int ab = 9;
System.out.println(b(ab));
}
}
1。類裏的方法之間可以互相調用,但靜態方法不能調實例方法
2。在類的外面可以直接調用這個類的public static method
而調用這個類的實例方法(public)就必須生成這個類的一個對象
public void b(){
……
}
public void c(){
a ac=new a();
a.b();
}
}或者public class a{
static{
b();
}
public static void b(){
}
}
验证当然没问题拉~!可是我怕有特殊情况啊~!
我就是希望知道正确的理论是什么啊!是啊就是书没有看透啊~!
看了有些地方不明白才来这里问的~!
对不起啊
public static void staticMethod() {
System.out.println("staticMethod");
}
public void instanceMethod() {
System.out.println("instanceMethod");
}
public static void staticMethod2() {
staticMethod();
}
}
public class B {
public static void main(String[] args) {
String sm = A.staticMethod();//钡秸ノclass A public static method
A a = new A();
String im = a.instanceMethod();//ネΘclass A 龟ㄒ秸ノclass A 龟ㄒよ猭
System.out.println("sm: " + sm);
System.out.println("im: " + im);
}
}
我的method b是靜態的,呵呵
public class a{
public void b(){
……
}
public void c(){
b();
}
}
直接调用b()就可以了吧
非静态方法里可以调用同一个类里的静态方法和非静态方法
静态方法里只能直接调用静态方法了
如果真想把Java的語法學透,看看Scjp的練習,很多平時用不到的語法~~最好別看,呵呵
class A {
public void method() {
System.out.println("A method");
}
}
class B extends A{
public void method() {
System.out.println("B method");
}
}
public Test {
public static void main(String[] args) {
B b = new B();
b.method(); //print B method A a = new A();
a.method(); //print A method A a1 = new B();
a1.method(); //print B method
}
}
如果 method都是靜態方法,
A a1 = new B();
a1.method();
輸出結果會不同~~