面向对象oop(三)

1.内存管理:由JVM来管理----------------了解
  1)堆:
    1.1)用于存储所有new出来的对象(包括成员变量)
1.2)垃圾:没有任何引用所指向的对象
    垃圾回收器(GC)不定时到内存中清扫垃圾,
回收过程中是透明的,不一定一发现垃圾就立即回收
调用System.gc()可以建议虚拟机快速调度GC来回收
1.3)内存泄漏:不再使用的内存没有被及时的回收
    建议:对象不再使用时应及时将引用设置为null
1.4)成员变量的生命周期:
      创建对象时存储在堆中,对象被回收时一并被回收
  2)栈:
    2.1)存储正在调用的方法中的所有局部变量(包括参数)
2.2)调用方法中在栈中为该方法分配一块对应的栈帧,
    栈帧中存储方法中的所有局部变量(包括参数),
方法执行结束时,栈帧被清除,局部变量一并被清除
2.3)局部变量的生命周期:
      调用方法中存在栈中,方法结束时与栈帧一并被清除
  3)方法区:
    3.1)存储.class字节码文件(包括方法)
3.2)方法只有一份,通过this来区分具体的对象
2.继承:
  1)实现代码的复用
  2)通过extends来实现继承
  3)父类/基类:所有子类所共有的属性和行为
    子类/派生类:子类所特有的属性和行为
  4)子继承父之后,子具有: 父+子
  5)一个父类可以有多个子类
    一个子类只能继承一个父类-------单一继承
  6)继承具有传递性
  7)java规定:构造子类之前必须先构造父类
    子类构造方法中若不调用父类的构造方法,则默认super()调用父类的无参构造
若子类构造中调用了父类的构造方法,则不再默认提供
super()调父类构造方法必须位于子类构造的第一句
3.super:指代当前对象的父类对象
  super的用法:
    1)super.成员变量名------------访问父类的成员变量
2)super.方法名()--------------调用父类的方法
3)super()---------------------调用父类的构造方法
4.向上造型:
  1)父类型的引用指向子类的对象

  2)能点出来什么,看引用的类型

package oo.day03;
//格子类
public class Cell {
	int row; //行号
	int col; //列号
	Cell(){
		this(0,0); //调用构造方法
	}
	Cell(int n){
		this(n,n); //调用构造方法
	}
	Cell(int row,int col){
		this.row = row;
		this.col = col;
	}
	
	void drop(){ //下落1格
		row++;
	}
	void moveLeft(int n){ //左移n格
		col-=n;
	}
	String getCellInfo(){ //获取行号和列号
		return row+","+col;
	}
	
	void drop(int n){ //下落n格
		row+=n;
	}
	void moveLeft(){ //左移1格
		col--;
	}
	
}


















package oo.day03;
//四格拼板
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.day03;
//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);
	}
}















package oo.day03;
//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);		
	}
}


















package oo.day03;
//T型与J型的测试类
public class TJTest {
	public static void main(String[] args) {
		Tetromino o1 = new T(2,5); //向上造型
		printWall(o1); //先造型后传值
		
		J o2 = new J(1,4);
		printWall(o2); //传值的同时造型
	}
	
	//打墙+打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();
		}
	}
	
}



















package oo.day03;
//向上造型的演示
public class UpDemo {
	public static void main(String[] args) {
		Eoo o1 = new Eoo();
		o1.e = 1;
		o1.show();
		//o1.f = 2; //编译错误,父不能访问子
		
		Foo o2 = new Foo();
		o2.f = 1;
		o2.test();
		o2.e = 2;  //正确,子可以访问父的
		o2.show();
		
		Eoo o3 = new Foo(); //向上造型
		o3.e = 1;
		o3.show();
		//o3.f = 2; //编译错误,能点出来什么,看引用的类型
		
	}
}

class Eoo{
	int e;
	void show(){}
}
class Foo extends Eoo{
	int f;
	void test(){}
}
























猜你喜欢

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