面向对象oop(四)

1.方法的重写(Override):

         重新写、覆盖。

       1)发生在父子类中,方法名称相同,参数列表相同,方法体不同。

        2)重写方法被调用时,看对象的类型

2.重写与重载的区别:---------常见的面试题
      1)重写(Override):
            1.1)发生在父子类中,方法名称相同,参数列表相同,方法体不同。
    1.2)遵循"运行期绑定",看对象的类型调用方法。
      2)重载(Overload):
            2.1)发生在同一类中,方法名称相同,参数列表不同,方法体不同
    2.2)遵循"编译期绑定",看引用的类型绑定方法。
3.package:
      1)作用:避免类的命名冲突
      2)包一般都是有层次结构的
      3)类名的全称为: 包名.类名
      4)建议:包名所有字母都小写
  import:
      1)同一个包中的类可以直接访问,
        不同包中的类不能直接访问,想访问有两种方式:
1.1)先import声明类再直接访问----建议
1.2)类的全称---------------------太繁琐,不建议
4.访问控制修饰符:
  1)public:公开的,任何类
  2)private:私有的,本类
  3)protected:受保护的,本类、子类、同包类
  4)默认的:什么也不写,本类、同包类
  类的访问修饰为: public和默认的

  类中的成员的访问修饰为:如上4种都可以

作用域  当前类  同一package  子孙类  其他package 
public       √               √                 √           √ 
protected √               √                 √           × 
default       √              √                  ×          × 
private      √              ×                  ×           ×

5.static:静态的
  1)静态变量:
    1.1)由static修饰
1.2)属于类,存在方法区中,只有一份
1.3)常常通过类名点访问
1.4)何时用:所有对象共享的数据(图片、音频、视频等)
  2)静态方法:
    2.1)由static修饰
2.2)属于类,存在方法区中,只有一份
2.3)常常通过类名点来访问
2.4)静态方法没有隐式的this传递
    所以静态方法中不能直接访问实例成员
2.5)何时用:方法的操作仅与参数相关而与对象无关
  3)静态块:
    3.1)属于类的,在类被加载期间自动执行,
    因类只被加载一次,所以静态块也只执行一次
3.2)何时用:常常用于加载/初始化静态资源(图片、音频、视频等)
6.final:最终的、不可改变的
  1)修饰变量:变量不可被改变
  2)修饰方法:方法不可被重写

  3)修饰类:类不可被继承

package oo.day04;
//static的演示
public class StaticDemo {
	public static void main(String[] args) {
		Loo o1 = new Loo();
		o1.show();
		Loo o2 = new Loo();
		o2.show();
		
		System.out.println(Loo.b); //建议类名点来访问
		//System.out.println(o1.b); //不建议通过对象来访问
		
		Moo.test();
	
		Noo o3 = new Noo();
		Noo o4 = new Noo();
		Noo o5 = new Noo();
	}
}


class Noo{ //演示静态块
	Noo(){ //创建对象时被自动执行
		System.out.println("构造方法");
	}
	static{ //类被加载时自动执行
		System.out.println("静态块");
	}
}


class Moo{ //演示静态方法
	int a;
	static int b;
	void show(){ //有隐式的this
		System.out.println(+a); //this.a
		System.out.println(b); //Moo.b
	}
	static void test(){ //没有隐式的this
		//静态方法没有隐式的this,
		//没有this就意味着没有对象,
		//而实例成员必须通过对象来访问
		//静态方法中不能直接访问实例成员
		//System.out.println(a); //编译错误
		System.out.println(b); //Moo.b
	}
}


class Loo{ //演示静态变量
	int a; //实例变量
	static int b; //静态变量
	Loo(){
		a++;
		b++;
	}
	void show(){
		System.out.println("a="+a);
		System.out.println("b="+b);
	}
}












package oo.day04;
//final的演示
public class FinalDemo {
	public static void main(String[] args) {
		
	}
}


//演示final修饰类
final class Roo{}
//class Soo extends Roo{} //编译错误,final的类不可被继承
class Too{}
final class Uoo extends Too{}


//演示final修饰方法
class Poo{ 
	void show(){}
	final void test(){}
}
class Qoo extends Poo{
	void show(){}
	//void test(){} //编译错误,final的方法不可被重写
}

/*
 * final修饰成员变量,两种初始化方式:
 *   1)声明的同时初始化
 *   2)在构造方法中初始化
 * final修饰局部变量,用之前初始化即可
 */

class Ooo{ //演示final修饰变量
	final int a = 5;
	final int b;
	Ooo(){
		b = 6;
	}
	void show(){
		final int c;
		//a = 55; //编译错误,final的变量不可被改变
	}
}















package oo.day04;
//重写的演示
public class OverrideDemo {
	public static void main(String[] args) {
		/*
		Boo o1 = new Boo();
		o1.show(); //子show
		
		Aoo o2 = new Boo(); //向上造型
		o2.show(); //子show
		*/
	
		//重载看引用类型,重写看对象类型
		Goo goo = new Goo();
		Eoo o = new Foo(); //向上造型
		goo.test(o); //重载看引用的类型(Eoo)绑定方法
	}
}
class Goo{
	void test(Eoo o){
		System.out.println("父型参数");
		o.show(); //重写看对象的类型(Foo)调用方法
	}
	void test(Foo o){
		System.out.println("子型参数");
		o.show();
	}
}
class Eoo{
	void show(){
		System.out.println("父类show");
	}
}
class Foo extends Eoo{
	void show(){
		System.out.println("子类show");
	}
}










/*
 * 重写要遵循"两同两小一大"原则:
 * 1)两同:
 *   1.1)方法名相同
 *   1.2)参数列表相同
 * 2)两小:
 *   2.1)子类方法的返回值类型小于或等于父类的
 *       2.1.1)void时,必须相同
 *       2.1.2)基本类型时,必须相同
 *       2.1.3)引用类型时,小于或等于
 *   2.2)子类方法抛出的异常小于或等于父类的------异常之后
 * 3)一大:
 *   3.1)子类方法的访问权限大于或等于父类的---访问控制修饰符后
 */

//父类大,子类小
class Coo{
	void show(){}
	double say(){return 0.0;}
	Doo sayHi(){return null;}
	public Coo test(){return null;}
}
class Doo extends Coo{
	//int show(){return 1;} //编译错误,void时必须相同
	//int say(){return 0.0;} //编译错误,基本类型时必须相同
	//Coo sayHi(){return null;} //编译错误,引用类型必须小于或等于
	public Doo test(){return null;}
}















class Aoo{
	void show(){
		System.out.println("父类show");
	}
}
class Boo extends Aoo{
	void show(){
		System.out.println("子类show");
	}
}

















package oo.day04;
//J型
public class J extends Tetromino {
	J(){
		this(0,0);
	}
	J(int row,int col){
		super();
		cells[0] = new Cell(row,col);
		cells[1] = new Cell(row+1,col);
		cells[2] = new Cell(row+2,col-1);
		cells[3] = new Cell(row+2,col);		
	}

	void print(){ //输出4个格子的行号和列号
		System.out.println("I am a J:");
		super.print();
	}
}



















package oo.day04;
//T型
public class T extends Tetromino {
	T(){
		this(0,0);
	}
	T(int row,int col){
		super(); //默认--调用父类的无参构造方法
		super.cells[0] = new Cell(row,col); //创建Cell对象
		super.cells[1] = new Cell(row,col+1);
		super.cells[2] = new Cell(row,col+2);
		super.cells[3] = new Cell(row+1,col+1);
	}

	void print(){ //输出4个格子的行号和列号
		System.out.println("I am a T:");
		super.print(); //调用父类的print()方法
	}
}















package oo.day04;
//四格拼板
public class Tetromino {
	Cell[] cells; //格子数组
	Tetromino(){
		cells = new Cell[4]; //创建Cell数组对象
	}
	
	void drop(){
		for(int i=0;i<this.cells.length;i++){
			this.cells[i].row++;
		}
	}
	void moveLeft(){
		for(int i=0;i<this.cells.length;i++){
			this.cells[i].col--;
		}
	}
	void moveRight(){
		for(int i=0;i<this.cells.length;i++){
			this.cells[i].col++;
		}
	}
	void print(){ //输出4个格子的行号和列号
		for(int i=0;i<this.cells.length;i++){
			String str = this.cells[i].getCellInfo();
			System.out.println(str);
		}
	}
}












package oo.day04;
//T型与J型的测试类
public class TJTest {
	public static void main(String[] args) {
		Tetromino t = new T(2,5);
		t.print();
		
		Tetromino j = new J(1,4);
		j.print();
	}
	
	//打墙+打T形
	public static void printWall(Tetromino t){
		//效率低、扩展性好
		for(int i=0;i<20;i++){
			for(int j=0;j<10;j++){

				boolean flag = false; //1.假设打-
				for(int k=0;k<t.cells.length;k++){
					if(i==t.cells[k].row && j==t.cells[k].col){
						flag = true; //2.修改为打*
						break;
					}
				}
				if(flag){ //3.判断得结果
					System.out.print("* ");
				}else{
					System.out.print("- ");
				}
				
			}
			System.out.println();
		}
	}
	
}




















猜你喜欢

转载自blog.csdn.net/qq_41264674/article/details/80337769