1.内存管理:由JVM来管理的
1)堆:
1.1)存储所有new出来的对象(包含成员变量)
1.2)没有任何引用所指向的对象就是垃圾,
垃圾回收器(GC)不定时清理垃圾,
回收过程是透明的(看不到的),
调用System.gc()建议GC尽快来回收
1.3)成员变量的生命周期:
创建对象时存在堆中,对象被回收时一并被回收
1.4)内存泄露:不再使用的对象没有被及时的回收
建议:对象不再使用时及时将引用设置为null
2)栈:
2.1)用于存储正在调用中方法的所有局部变量(包括参数)
2.2)调用方法时在栈中为该方法分配一块对应的栈帧,栈帧中存储的是方法中的所有局部变量(包括参数),
方法调用结束时,栈帧被清除,局部变量一并被清除
2.3)局部变量的生命周期:
调方法时存在栈中,方法结束时与栈帧一起被清除
3)方法区:
3.1)用于存储.class字节码文件(包括方法)
3.2)方法只有一份,
通过this来区分具体的对象
2.继承:
2.1)作用:实现代码的复用,避免代码的重复
2.2)通过extends来实现继承
2.3)父类:所有子类所共有的属性和行为
子类:子类所特有的属性和行为
2.4)子继承父后,子具有: 父类的+子类的
2.5)一个父类可以有多个子类,
一个子类只能有一个父类—-单一继承
2.6)继承具有传递性
2.7)java规定,构造子类之前必须先构造父类
若子类构造中不调用父类的构造,则默认super()调用父类的无参构造
若子类构造中调了父类的构造,则不再默认提供
super()调用父类的构造必须位于子类构造的第一句
3.super:指代当前对象的父类对象
super的用法:
1)super.成员变量名——访问父类的成员变量
2)super.方法名()——–调用父类的方法
3)super()—————调用父类的构造方法
4.向上造型:
1)父类型的引用指向子类的对象
2)能点出来什么,看引用的类型
//老虎是动物
Animal o3 = new Tiger(); //向上造型
class Animal{ //动物类
}
class Tiger extends Animal{ //老虎类
}
JAVA内存变量:
编译期,只检查语法是否正确
编译错误,指的是语法错误
运行期,JVM登场—-分配内存
变量—-占用的是JVM的内存
JAVA垃圾管理:
没有任何引用所指向的对象
垃圾回收器(GC),不定时到内存堆中清理垃圾
注:并非看到垃圾就立刻清理
垃圾回收过程是透明的(看不到的)
System.gc()让内存回收尽量快一些
内存泄露:
没有用的内存还没有被及时的回收
若对象不用了,及时将引用设置为null
super的演示:
package oo.day03;
//super的演示
public class SuperDemo {
public static void main(String[] args) {
Boo o = new Boo();
}
}
class Coo{
Coo(int a){
}
}
class Doo extends Coo{
Doo(){
super(5);
}
}
class Aoo{
Aoo(){
System.out.println("父类构造方法");
}
}
class Boo extends Aoo{
Boo(){
super(); //默认的
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(){}
}
Tetromino:
cell:
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(){ //下落一格
row++; //4,3
}
void moveLeft(int n){ //左移n格
col-=n;
}
String getCellInfo(){ //获取行号和列号
return row+","+col;
}
void drop(int n){ //下落n格
row+=n;
}
void moveLeft(){ //左移一格
col--;
}
}
CellTest:
package oo.day03;
//格子类的测试类
public class CellTest {
public static void main(String[] args) {
/*
Cell c = new Cell();
c.row = 2;
c.col = 5;
c.drop(); //下落一格
c.drop(3); //下落三格
c.moveLeft(); //左移一格
c.moveLeft(2); //左移两格
printWall(c);
*/
Cell c1 = new Cell(); //调无参
Cell c2 = new Cell(3); //调一个参数
c2.drop();
Cell c3 = new Cell(2,5); //调两个参数
printWall(c2);
}
//打墙+打格
public static void printWall(Cell cc){
for(int i=0;i<20;i++){ //行
for(int j=0;j<10;j++){ //列
if(i==cc.row && j==cc.col){ //行列匹配
System.out.print("* ");
}else{
System.out.print("- ");
}
}
System.out.println(); //换行
}
}
}
Tetromino:
package oo.day03;
//四格拼板
public class Tetromino {
Cell[] cells; //格子数组
Tetromino(){
cells = new Cell[4];
}
void drop(){
for(int i=0;i<this.cells.length;i++){
this.cells[i].row++;
}
}
void moveLeft(){
for(int i=0;i<cells.length;i++){
cells[i].col--;
}
}
void moveRight(){
for(int i=0;i<cells.length;i++){
cells[i].col++;
}
}
void print(){
for(int i=0;i<this.cells.length;i++){
String str = this.cells[i].getCellInfo();
System.out.println(str);
}
}
}
T:
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); //创建格子对象
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);
}
}
J:
package oo.day03;
//J型
public class J extends Tetromino {
J(){
this(0,0);
}
J(int row,int col){
cells[0] = new Cell(row,col);
cells[1] = new Cell(row,col+1);
cells[2] = new Cell(row,col+2);
cells[3] = new Cell(row+1,col+2);
}
}
TJTest:
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();
}
}