编码练习——Java-6-类的高级特性

基础代码

  • 定义抽象类
abstract class Fruit {
    public String color;
    public Fruit() {
        color = "绿色";
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 定义抽象方法 
    • 抽象类:Fruit
    • 抽象方法:harvest()
public class Test {
    public static void main(String[] args) {
        System.out.println("调用Apple类的harvest()方法的结果是:");
        Apple apple = new Apple();
        apple.harvest();
        System.out.println("嗲用Orange类的harvest()方法的结果是:");
        Orange orange = new Orange();
        orange.harvest();
    }
}

abstract class Fruit {
    public String color;
    public Fruit() {
        color = "绿色";
    }
    public abstract void harvest(); //抽象方法
}

class Apple extends Fruit {
    @Override
    public void harvest() {
        System.out.println("苹果已经收获!");
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 成员内部类
public class OuterClass {
    innerClass in = new innerClass();
    public void ouf() {
        in.inf();
    }
    class innerClass {
        innerClass() {
        }
        public void inf() {
        }
        int y =  0;
    }
    public innerClass doit() {
        in.y = 4;
        return new innerClass();
    }

    public static void main(String[] args) {
        OuterClass out = new OuterClass();
        //内部类的对象实例化操作必须在外部类或外部类中的非静态方法中实现
        OuterClass.innerClass in = out.doit();
        OuterClass.innerClass in2 = out.new innerClass(); //实例化内部类对象
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 获取内部类和外部类的引用
public class OuterClass {
    private int x;
    private class Inner {
        private int x=9;
        public void doit(int x) {
            x++;    //调用形参x
            this.x++;   //调用内部类的变量
            OuterClass.this.x++;    //调用外部类的变量x
        }
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 局部内部类
public class SellOutClass {
    private String name;
    public SellOutClass() {
        name = "苹果";
    }
    public void sell(int price) {
        class Apple{
            int innerPrice=0;
            public Apple(int price) {
                innerPrice = price;
            }
            public void price() {
                System.out.println("现在开始销售" + name);
                System.out.println("单价为:" + innerPrice + "元");
            }
        }
        Apple apple = new Apple(price);
        apple.price();
    }
    public static void main(String[] args) {
        SellOutClass sample = new SellOutClass();
        sample.sell(100);
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 匿名内部类
public class OutString {
    public static void main(String[] args) {
        final String sourceStr = "吉林省 明日 科技有限公司 —— 编程 词典!";
        IStringDeal s = new IStringDeal() { //匿名内部类
            @Override
            public String filterBlankChar() {
                String convertStr = sourceStr;
                convertStr = convertStr.replaceAll(" ", "");
                return convertStr;
            }
        };
        System.out.println("源字符串:" + sourceStr);
        System.out.println("转换后的字符串:" + s.filterBlankChar());
    }
}

interface IStringDeal { 
    public String filterBlankChar();
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 静态内部类
public class StaticInnerClass {
    static int x = 100;
    static class Inner {
        static void doitInner() {
            System.out.println("外部类的成员变量"+x);
        }
        public static void main(String[] args){
            doitInner();
        }
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 内部类的继承
public class OutputInnerClass extends ClassA.ClassB {
    public OutputInnerClass(ClassA a) {
        a.super();
    }
}

class ClassA {
    class ClassB {

    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 实例 - 静态内部类求极值
public class Test {
    public static void main(String[] args) {
        double[] array = new double[5];
        for (int i=0; i<array.length; i++) {
            array[i] = 100*Math.random();
        }
        System.out.println("源数组:");
        for(int i=0; i<array.length; i++) {
            System.out.print(array[i] + "  ");
        }
        System.out.println();
        System.out.println("最大值:" + MaxMin.getResult(array).getMax());
        System.out.println("最小值:" + MaxMin.getResult(array).getMin());
    }
}

class MaxMin {
    public static class Result {
        private double max;
        private double min;
        public Result(double max, double min) {
            this.max = max;
            this.min = min;
        }
        public double getMax() {
            return max;
        }
        public double getMin() {
            return min;
        }
    }
    public static Result getResult(double[] array) {
        double max = Double.MIN_VALUE;
        double min = Double.MAX_VALUE;
        for (double i : array) {
            if (i>max)
                max = i;
            if (i<min)
                min = i;
        }
        return new Result(max, min);
    }
}
from: http://blog.csdn.net/xuezhisdc/article/details/52190671
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值