局部内部类
package ch6;
/**
* Created by Jiqing on 2016/11/21.
*/
public class LocalInnerClass { // 局部内部类
public static void main(String[] args) {
// 定义局部内部类
class InnerBase {
int a;
}
// 定义局部内部类的子类
class InnerSub extends InnerBase
{
int b;
}
// 创建局部内部类对象
InnerSub is = new InnerSub();
is.a = 5;
is.b = 10;
System.out.println("InnerSub对象的a和b实例变量是:" + is.a + "," + is.b);
}
}
强制回收
package ch6;
/**
* Created by Jiqing on 2016/11/21.
*/
public class Gc {
public static void main(String[] args) {
for(int i = 0; i<4;i++) {
new Gc();
// System.gc(); // 强制回收内存
Runtime.getRuntime().gc();
}
}
public void finalize() {
System.out.println("正在回收内存");
}
}
抽象类与抽象函数
package ch6.Shape;
/**
* Created by Jiqing on 2016/11/21.
*/
public abstract class Shape {
private String color;
public abstract double calPerimeter();
public abstract String getType();
public Shape() {}
public Shape(String color) {
System.out.println("执行Shape构造器...");
this.color = color;
}
}
package ch6.Shape;
/**
* Created by Jiqing on 2016/11/21.
*/
public class Triangle extends Shape {
private double a;
private double b;
private double c;
public Triangle(String color,double a,double b,double c){
super(color);
this.setSides(a,b,c);
}
public void setSides(double a,double b,double c){
if (a >= b + c || b >= a + c|| c >= a + b) {
System.out.println("三角形两边之和大于第三边...");
return;
}
this.a = a;
this.b = b;
this.c = c;
}
public double calPerimeter() {
return a + b + c;
}
public String getType() {
return "三角形";
}
public static void main(String[] args) {
Shape t = new Triangle("黑色",3,4,5);
System.out.println(t.calPerimeter());
System.out.println(t.getType());
}
}
枚举类
package ch6.Enum;
/**
* Created by Jiqing on 2016/11/21.
*/
public enum SeasonEnum { // 枚举类
SPRING,SUMMER,FALL,WINTER;
}
package ch6.Enum;
/**
* Created by Jiqing on 2016/11/21.
*/
public class Enum {
public void judge(SeasonEnum s) {
switch (s) {
case SPRING:
System.out.println("春暖花开,正好踏青");
break;
case SUMMER:
System.out.println("夏日炎炎,适合游泳");
break;
case FALL:
System.out.println("秋高气爽,进补及时");
break;
case WINTER:
System.out.println("冬天寒冷,被窝赏雪");
}
}
public static void main(String[] args) {
for (SeasonEnum s : SeasonEnum.values()) {
System.out.println(s);
}
new Enum().judge(SeasonEnum.FALL);
}
}
package ch6.Enum;
/**
* Created by Jiqing on 2016/11/21.
*/
public enum Operation { // 编译程序会生产5个class文件
PLUS{ // 匿名内部子类
public double eval(double x,double y) {
return x + y;
}
},
MINUS {
public double eval(double x,double y) {
return x - y;
}
},
TIMES {
public double eval(double x,double y) {
return x * y;
}
},
DIVIDE {
public double eval(double x,double y) {
return x/y;
}
};
public abstract double eval(double x,double y);
public static void main(String[] args) {
System.out.println(Operation.DIVIDE.eval(1.2,2.3));
}
}