One, access permission modifier
1. Modification of attributes and methods
Inside the class
Inside the bag
Subclass
Outer package
default
√
√
public
√
√
√
√
private
√
protected
√
√
√
2. Modification
Inside the class
Inside the bag
Subclass
Outer package
default
√
√
public
√
√
√
√
private
√
protected
√
Some bits and pieces of knowledge
The class modified by private and protected must be an inner class. For independently defined classes, generally cannot be defined as private or protected
Inner classes can only be accessed inside the class where they are defined
Inner class cannot be used as super class
A class without any modification is called a friendly class
When creating objects using friendly classes in another class, you need to make sure they are in the same package
Exercise
PA
package PackageA;
public class PA {
protected int id;
protected String name;
String sex;
private class PA1
{
public void print ( )
{
System. out. println ( "在PA类内部定义的私有类PA1,仅供PA访问" ) ;
}
}
protected class PA2
{
public void print ( )
{
System. out. println ( "在PA类内部定义的保护类PA2,仅供PA访问" ) ;
}
}
public PA ( int i, String s)
{
id= i;
name= s;
}
protected int getId ( )
{
return id;
}
String getSex ( )
{
return sex;
}
public String getName ( )
{
return name;
}
public void print ( )
{
System. out. println ( "id:" + id) ;
System. out. println ( "Name:" + name) ;
System. out. println ( "Sex:" + sex) ;
}
public void print1 ( )
{
PA1 pa1= new PA1 ( ) ;
pa1. print ( ) ;
PA2 pa2= new PA2 ( ) ;
pa2. print ( ) ;
}
}
PB
package PackageA;
public class PB {
public static void main ( String[ ] args)
{
PA a= new PA ( 1 , "张三" ) ;
a. print ( ) ;
a. id= 2 ;
a. name= "李四" ;
System. out. println ( "id:" + a. getId ( ) ) ;
System. out. println ( "name:" + a. getName ( ) ) ;
}
}
operation result
Test
package PackageB;
import PackageA. *;
public class TestProtected {
public static void main ( String[ ] args)
{
PA a= new PA ( 1 , "张三" ) ;
a. print ( ) ;
System. out. println ( "Name:" + a. getName ( ) ) ;
}
}
operation result
Second, the method of parameter transfer
Example
package PackageA;
public class Para {
String value;
public static void value ( String a, String b, String c, String d)
{
a= a. concat ( b) ;
c= b;
d= new String ( a) ;
System. out. println ( "改变变量值之后:a=" + a+ ",b=" + b+ ",c=" + c+ ",d=" + d) ;
}
public static void change ( Para a, Para b, Para c, Para d)
{
a. value= a. value. concat ( b. value) ;
c. value= b. value;
d. value= new String ( a. value) ;
System. out. println ( "改变变量值之后:a=" + a+ ",b=" + b+ ",c=" + c+ ",d=" + d) ;
}
@Override
public String toString ( )
{
return value;
}
public static void main ( String[ ] args)
{
String a, b, c, d;
a= "aaa" ;
b= "bbb" ;
c= "ccc" ;
d= "ddd" ;
System. out. println ( "调用value之前:a=" + a+ ",b=" + b+ ",c=" + c+ ",d=" + d) ;
value ( a, b, c, d) ;
System. out. println ( "调用value之后:a=" + a+ ",b=" + b+ ",c=" + c+ ",d=" + d) ;
Para a1, b1, c1, d1;
a1= new Para ( ) ;
b1= new Para ( ) ;
c1= new Para ( ) ;
d1= new Para ( ) ;
a1. value= "aaaa" ;
b1. value= "bbbb" ;
c1. value= "cccc" ;
d1. value= "dddd" ;
System. out. println ( "调用change之前:a1=" + a1+ ",b1=" + b1+ ",c1=" + c1+ ",d1=" + d1) ;
change ( a1, b1, c1, d1) ;
System. out. println ( "调用change之后:a1=" + a1+ ",b1=" + b1+ ",c1=" + c1+ ",d1=" + d1) ;
}
}
operation result