欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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


相关标签: Java面向对象