1、首先,认清这四类成员变量以及由这些关键修辞成员变量和成员方法形成的四类成员方法。
2、四种类型的成员变量:
3、公共静态数据类型成员变量名;-公共的静态成员变量。
(资料图片仅供参考)
4、公共数据类型成员变量名;-公共的非静态成员变量。
5、私有静态数据类型成员变量名;-私有的静态成员变量。
6、私有数据类型成员变量名;-私有非静态成员变量。
7、第4类成员方法:
8、公共静态数据类型成员方法名(){ }-公共静态成员方法。
9、公共数据类型成员方法名称(){ }-公共的非静态成员方法。
10、私有静态数据类型成员方法名称(){ }-私有静态成员方法。
11、私有数据类型成员方法名称(){ }-私有非静态成员方法。
12、其次,您应该知道创建的成员更改只有在方法中被访问时才能生效。另外,原则上一个方法可以调用另一个方法。但是有了这些关键字修辞成员变量和成员方法,可以在另一个方法中访问吗?
13、被叫怎么办?
14、以下是一些例子
15、step1:新建一个类(本例类名:MethodVisitMethod)。8个成员方法,1个主方法main() 它是用来测试验证。
16、
17、step2:代码如下
18、/**
19、 * 本实例探究一个方法访问其它成员方法有什么的规律
20、*/
21、public class MethodVisitMethod {
22、 //create 主方法
23、 public static void main(String[] args) {
24、 System.out.println("综合测试结果,得出如下结论:");
25、 System.out.println("1.静态的方法只能调用到静态的成员方法。");
26、 System.out.println("2.非静态的方法可以调用到静态的和非静态的方法。");
27、 }
28、 //create 公有、静态的方法
29、 public static void show_1() {
30、 int a=10;
31、 int b=20;
32、 int c=a + b;
33、 System.out.println(c);
34、 }
35、 //create 私有、静态的方法
36、 private static void show_2() {
37、 int a=10;
38、 int b=20;
39、 int c=a * b;
40、 System.out.println(c);
41、 }
42、 //create 公有的、非静态的方法
43、 public void show_3() {
44、 int a=10;
45、 int b=20;
46、 int c=a/b;
47、 System.out.println(c);
48、 }
49、 //create 私有、非静态的方法
50、 private void show_4() {
51、 int a=10;
52、 int b=20;
53、 int c=a - b;
54、 System.out.println(c);
55、 }
56、 //create 测试方法
57、 public static void test_1() {
58、 show_1();
59、 show_2();
60、 //show_3();//无法从静态上下文中引用非静态方法show_3()
61、 //show_4();//无法从静态上下文中引用非静态方法show_4()
62、 //说明公有、静态的方法只能调用静态的方法。
63、 }
64、 private static void test_2() {
65、 show_1();
66、 show_2();
67、 //show_3();//无法从静态上下文中引用非静态方法show_3()
68、 //show_4();//无法从静态上下文中引用非静态方法show_4()
69、 //说明私有、静态的方法只能调用静态的方法。
70、 }
71、 public void test_3() {
72、 show_1();
73、 show_2();
74、 show_3();
75、 show_4();
76、 //说明公有、非静态的方法可以调用静态的和非静态的方法。
77、 }
78、 private void test_4() {
79、 show_1();
80、 show_2();
81、 show_3();
82、 show_4();
83、 //说明私有、非静态的方法可以调用静态的和非静态的方法。
84、 }
85、 /*
86、 *
87、 * */
88、}
89、step3:新建一个类(本例类名:MethodVisitVariable)
90、本类由4个成员变量,4个成员方法,1个主方法main()组成
91、代码如下
92、/**
93、* 本实例探究一个方法访问成员变量的规律
94、*/
95、public class methodVisitVariable {
96、 public static void main(String[] args){
97、 System.out.println("综合测试结果,可以得出结论:");
98、 System.out.println("1.静态方法只能访问静态的成员变量。");
99、 System.out.println("2.非静态的方法可以访问静态的和非静态的成员变量。");
100、 }
101、 //satement variable
102、 public static String a1;
103、 private static String a2;
104、 public String a3;
105、 private String a4;
106、 //create method
107、 public static void call_1(){
108、 String a=a1;
109、 String b=a2;
110、 //String c=a3;
111、 //String d=a4;
112、 //说明公有、静态的方法只能访问静态的成员变量。
113、 }
114、 //create method
115、 private static void call_2(){
116、 String a=a1;
117、 String b=a2;
118、 //String c=a3;
119、 //String d=a4;
120、 //说明私有、静态的方法只能访问静态的成员变量。
121、 }
122、 //create method
123、 public void call_3(){
124、 String a=a1;
125、 String b=a2;
126、 String c=a3;
127、 String d=a4;
128、 //说明公有、非静态的方法可以访问静态的和非静态成员变量。
129、 }
130、 //create method
131、 private void call_4(){
132、 String a=a1;
133、 String b=a2;
134、 String c=a3;
135、 String d=a4;
136、 //说明私有、非静态的方法可以访问静态的和非静态成员变量。
137、 }
138、}
139、 /*测试方法是逐个方法进行测试,取消前各个被调用的方法前面的注释符//,运行main方法。对多次行动的结果进行归纳总结:得出如下规律:(如图所示)
140、*/
本文到此结束,希望对大家有所帮助。