package com.yfs.javase; public class ParamDemo { public static void main(String[] args) { int a = 3, b = 5; change(a,b); System.out.println("a = " + a + " b = " + b); Point p = new Point(); p.x = 10; p.y = 20; change(p); System.out.println("p.x = " + p.x + " p.y = " + p.y); } public static void change(Point p) {//引用指向 是对象 int c = p.x; p.x = p.y; p.y = c; } public static void change(int a, int b) {//局部变量 传入是数值 System.out.println("================"); System.out.println("|||||| a = " + a + " b = " + b +"||||||"); int c = a; a = b; b = c; System.out.println("|||||| a = " + a + " b = " + b +"||||||"); System.out.println("================"); } } class Point { int x; int y; }
1.Person
package com.yfs.javase; public class Person { public Person() { System.out.println("javase.Person ***"); } }
2. other Person
package com.yfs.javase.other; public class Person { public Person() { System.out.println("other.Person 对象..."); } }
3.Person 测试
package com.yfs.javase.other; import com.yfs.javase.Person; public class PersonTest { /** * @param args */ public static void main(String[] args) { Person p1 = new Person(); com.yfs.javase.other.Person p2 = new com.yfs.javase.other.Person(); } }
4.default
package com.yfs.javase.other; class DefaultDemo { } class Other { } /*public*/ class Demo { }
5.公有类
package com.yfs.javase.other; //一个java文件只能有一个public 修饰的类 // 类与文件同名 public /*final*/ class PublicClass { private String name; public int age; protected char sex; int score ; static final int id ;//= 520;//只有一份 所有对象共享 static { id = 200; } public PublicClass() { //id = 100; } //方法的权限 public final void publicMethod() { //id = 350; } private void privateMethod() { protectedMethod(); } protected void protectedMethod() { } /*default*/ void defaultMethod() { protectedMethod(); } } /*default public*/ class DefaultClass { }
6.测试
package com.yfs.javase.other; //同一包下 public class AccessTest1 { public static void main(String[] args) { PublicClass pc = new PublicClass(); pc.publicMethod(); //pc.privateMethod();私有方法不能访问 pc.protectedMethod(); pc.defaultMethod(); pc.age = 20; pc.score = 90; } }
7.包外测试
package com.yfs.javase; import com.yfs.javase.other.PublicClass; //不同包下 public class AccessTest2 { public static void main(String[] args) { PublicClass pc = new PublicClass(); pc.publicMethod(); //pc.privateMethod();私有方法不能访问 //pc.protectedMethod();protected方法不能访问 //pc.defaultMethod();defautl方法不能访问 } } class Child extends PublicClass { public void test () { //privateMethod(); protectedMethod();//子类可以访问 //defaultMethod();//不同包不能访问 } // @Override // public final void publicMethod() { // // TODO Auto-generated method stub // // } }