静态办法:有static修饰的办法。
非静态办法:没有static修饰的办法。
- 办法调用:
1)一个类:直接调用。
2)不同类/不同文件:
a: 类名.属性名/办法名
b:实例化对象。 类名 对象名 = new类名();
对象名. 属性/办法
二静态调用 非静态办法/属性
都先实例化对象。 类名 对象名 = new类名();
对象名. 属性名/办法名
一非静态调用静态办法
二非静态调用非静态办法
1)同一类中:直接调用
2)不同类中:
a: 类名 . 办法(只能是静态属性)
b:实例化对象
总结:可直接调用的三种状况
1.一个类中 静态调静态 。
2. 一个类中 非静态调用 静态/非静态。
3. 静态 类名.静态属性/静态办法。
public class Demo03{ int age; public static void main(String []args){ System.out.println(Demo04.name);//静态调用静态1 Demo04.eat(); Demo04 d = new Demo04();//静态调用静态2 System.out.println(d.name); d.eat(); Demo03 d1 = new Demo03();//静态调用非静态 d1.method(); System.out.println(d1.age); } public void method(){ System.out.println("first method"); }}
1 public class Demo04{2 static String name = "张三";3 4 public static void eat(){5 System.out.println("肉夹馍");6 }7 }
1 public class Demo05{ 2 static int age; 3 String name; 4 public static void main(String []args){ 5 6 Demo05 d1 = new Demo05();//静态调非静态 实例化 7 d1.method(); 8 } 9 10 public void method(){11 System.out.println(age); //非静态调静态 12 method1(); //非静态调静态 13 System.out.println(name);//非静态调非静态 14 method2(); //非静态调非静态 15 System.out.println("first method");16 }17 public static void method1(){18 System.out.println("second method");19 }20 public void method2(){21 System.out.println("third method");22 }23 }
1 public class Demo06{ 2 3 public static void main(String []args){ 4 5 Demo06 d1 = new Demo06(); //静态调非静态 实例化 6 d1.method(); 7 } 8 public void method(){ 9 System.out.println(Person.name); //非静态调静态10 Person.method1(); //非静态调静态11 Person p = new Person(); //非静态调非静态 实例化12 p.method2();13 System.out.println("first method");14 }15 }16 class Person{17 static String name;18 int age;19 public static void method1(){20 System.out.println("second method");21 }22 public void method2(){23 System.out.println("third method");24 }25 }
自行获取
链接:https://pan.baidu.com/s/1QVji3U3g7Rw238Sdk9itJA
提取码:5mgn
--来自百度网盘超级会员V4的分享


雷达卡


京公网安备 11010802022788号







