面向对象和类
对象的引用
class test{
int iNum;
test(int i){
iNum=i;
}
public void getI(){
System.out.println(iNum);
}
public static void main(String[] args)
{
test a1=new test(5);
test a2=new test(6);
a1.getI();
a2.getI();
a2=a1; //对象的引用,将a1的指针赋给a2
a2.getI();
}
}
方法的重写
class A{
String strl="班长是:";
String name="hyandrew";
public void getName()
{
System.out.println(strl+name);
}
}
class B extends A{
String name="hongyawx";
public void getName() //方法的重写,与重载的区别是
{ //重写的返回类型,及参数必须完全一样
System.out.println(strl+name);
}
public void getN()
{ //super.name关键字表示调用父类的name变量
System.out.println(strl+super.name);
}
}
public class Extend{
public static void main(String []args)
{
B b=new B();
b.getName();
b.getN();
A a=new B(); //用父类实例化一个对象a,用子类B来初始化
//a 称为"上装型对象",不能调用子类新的方法getN
a.getName(); //调用class B的getName方法
}
}
访问控制符
访问控制符有public,private,protected,和默任,它们的权限如下
位置 | Private | 默认 | Protected | Public |
同一个类 | 是 | 是 | 是 | 是 |
同一个包内的类 | 否 | 是 | 是 | 是 |
不同包内的子类 | 否 | 否 | 是 | 是 |
不同包并不是子类 | 否 | 否 | 否 | 是 |
对“不同包内的子类”就是该类的子类在不同包;
class A
{
private int a;
protected int b = 5;
int c = 8; //c为默认类型,
public void setA(int a)
{
this.a = a;
}
public int getA() {
return a;
}
}
public class Control {
public static void main(String[] args) {
A a1 = new A();
a1.setA(4);
System.out.println(a1.getA()); //通过公有的方法访问
// System.out.print(a1.a) a是私有类型,不能直接访问
System.out.print(a1.c);
}
}
程序运行结果
静态变量
class StaticDemo {
static int x = 0;
StaticDemo() { x++; }
public static void main(String[] args)
{
System.out.println(StaticDemo.x); //x的值为0
StaticDemo d1 = new StaticDemo(); //实例化d1时对x进行了加一操作
StaticDemo d2 = new StaticDemo(); //再次加一,此时x为2
System.out.println(StaticDemo.x);
d1.x = 100;
StaticDemo.x = 200; //使用类名,直接调用静态变量
System.out.println(d2.x);
}
}
程序运行结果
静态方法
class StaticFun {
static int i = 10;
int j;
static void setValue(int x) {
//j=x; 该语句为错,静态方法只能访问静态变量
System.out.println(" " + i);
}
public static void main(String[] args) {
StaticFun.setValue(5); //直接调用静态方法
}
}
静态块
class jintaikuai {
static int[] Values = new int[10]; //定义静态数组
static { //静态块只在该类被加载时执行一次
for (int i = 0; i < Values.length; i++)
{
Values[i] = (int)(100.0 * Math.random());
}
}
public static void main(String[] args)
{
jintaikuai a1 = new jintaikuai();
for (int j = 0; j < Values.length; j++)
{
System.out.print(" " + a1.Values[j]);
}
}
}
Final修饰符
Final用在变量前面表示变量的值是不可变的,用在方法前面表示方法是不可重写的,用在类前面表示类是不可继承的
final class Fi { }
class Fin {
final int num = 5;
public final void getName() {
//如果加上"num=5;"这一句就错了,final表示的是对象的最终状态
//是不可改变的状态
System.out.println(num);
}
public void sumln() {
System.out.println(num + 10);
}
}
class Fina extends Fin {
//如果加上"public void getName(){};" 就错了,该方法已被定义为最终太
//不能被重写
public void sum()
{
System.out.println(num + 12);
}
}
public class Final {
public static void main(String[] args)
{
Fina f = new Fina();
f.getName();
f.sum();
}
}
接口
public class InterfaceDemo implements myinterface {
//创建继承该接口的类
public void add(int x, int y) { //在该类中实现接口的方法
System.out.println(" " + (x + y));
}
public void getA() {
System.out.println(a);
}
public static void main(String[] args)
{
InterfaceDemo d = new InterfaceDemo();
d.add(10, 20);
d.getA();
}
}
public interface myinterface
{
int a = 8; //接口中申明的变量相当于fianl修饰的,
//当成常量来用
public void add(int x, int y); //接口中的方法只能申明
}
注意接口myinterface与类interfacedemo应分开写分别编译,但要放在同一目录下