81,094
社区成员
发帖
与我相关
我的任务
分享
TestParent.class:
public class TestParent {
public static class ParentInner {
public static String pi1 = "pi1";
public static String pi2 = "pi2";
}
}
TestSub.class:
public class TestSub extends TestParent {
}
package com.eleven.oop;
/**
* 0.前面有关键字static,就叫静态方法,反之为非静态方法。
* 1.静态方法不能直接向对象实施操作。
* 2.静态方法是没有this参数的方法。(说明不是隐式参数)
* 3.静态方法可以访问自身类中的静态域。
* @author sywangu
*
*/
public class StaticMethod {
public static String getType() {
return "人类";
}
public String getName() {
return "张三";
}
public static void main(String[] args) {
// 01静态方法可以直接使用类名调用
String type = StaticMethod.getType();
System.out.println(type); // 人类
// 02非静态方法需要使用实例对象(StaticMethod)调用
StaticMethod method = new StaticMethod();
String name = method.getName();
String type1 = method.getType(); // 当然getType方法也可以通过对象进行调用
System.out.println("姓名:"+name+",类别:"+type1); // 姓名:张三,类别:人类
}
}