Java 面向对象:访问权private、public、常量、protected
程序员文章站
2024-02-18 16:50:16
...
package com.neusoft.pg;
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.neusoft.guess.Person;
/**
* 包:package 作用: 1.解决命名冲突的问题 2.进行分类
*
* 使用import关键字引入其它包中的类 import com.neusoft.guess.Person; 位于同一包中的类可以直接访问
*
* @author ttc
*
*/
public class A {
public static void main(String[] args) {
Person person = new Person();
String ss = "asdfadsf";
Button button = new Button("abc");
}
}
package AccessContral_mypack1;
public class A {
private int var1;
public int var2;
int var3;
protected int var4;
public void method() {
var1 = 1;
var2 = 1;
var3 = 1;
var4 = 1;
}
}
package AccessContral_mypack1;
public class B {
public static void main(String[] args) {
A a = new A();
a.var2 = 1;
a.var3 = 1;
a.var4 = 1;
// a.var1 = 1;
}
}
package AccessContral_mypack2;
import AccessContral_mypack1.A;
public class C extends A{
public static void main(String[] args) {
C c = new C();
c.var2 = 1;
c.var4 = 1;
}
}
package AccessContral_mypack2;
import AccessContral_mypack1.A;
public class D {
public static void main(String[] args) {
A a = new A();
a.var2 = 1;
}
}
推荐阅读
-
Java 面向对象:访问权private、public、常量、protected
-
php面向对象public private protected 访问修饰符_PHP教程
-
php 面向对象访问控制 public,private,protected详解
-
浅析php面向对象public private protected 访问修饰符
-
浅析php面向对象public private protected 访问修饰符
-
php 面向对象访问控制 public,private,protected详解_PHP教程
-
php 面向对象访问控制 public,private,protected详解
-
php面向对象public private protected 访问修饰符
-
php面向对象public private protected 访问修饰符_PHP教程
-
浅析php面向对象public private protected 访问修饰符_PHP教程