java 入门测试代码(二)

package com.company;

// 这里用了java包引用的语法
import com.apple.CBca;

import java.awt.*;
import java.io.*;


public class Main {
    public enum Note {
        MIDDLE_C, C_SHARP, B_FLAT; // Etc.
    }

    static void common_test() {
        CBca b = new CBca();
        b.test(1);

        CAbc abc = new CAbc();
        abc.test(12);

        int c = CAbc.add(1, 2);
        int a = 0;

        System.out.format("output:%d\n", c);
        abc.set(100);
        abc.set(1.1);

        ///
        // 枚举的调用
        abc.set(CAbc.Person_Enum.e_B);

        // 打印枚举的结果
        for (CAbc.Person_Enum ea : CAbc.Person_Enum.values()) {
            System.out.format("enum name:%s value:%d\n", ea.name(),
                    ea.ordinal());
        }

        // 打印单个的结果
        System.out.format("enum:%s %d\n", CAbc.Person_Enum.e_C.name(),
                CAbc.Person_Enum.e_C.ordinal());
        

        int out = abc.get();
        System.out.format("getvalue:%d %d arraysize:%d\n", out, abc.m_c, abc.m_array[2]);
    }

    // 组合的使用
    static void test() {
        class Engine {
            public void start() {
            }

            public void rev() {
            }

            public void stop() {
            }
        }

        class Wheel {
            public void inflate(int psi) {
            }
        }

        class Window {
            public void rollup() {
            }

            public void rolldown() {
            }
        }

        class Door {
            public Window window = new Window();

            public void open() {
            }

            public void close() {
            }
        }

        class Car {
            public Engine engine = new Engine();
            public Wheel[] wheel = new Wheel[4];
            public Door
                    left = new Door(),
                    right = new Door(); // 2-door

            public Car() {
                for (int i = 0; i < 4; i++)
                    wheel[i] = new Wheel();
            }
        }

        Car car = new Car();
        car.left.window.rollup();
        car.wheel[0].inflate(72);
    }

    // 调用顺序测试
    static void test2() {
        class Meal {
            Meal() {
                System.out.println("Meal()");
            }
        }

        class Bread {
            Bread() {
                System.out.println("Bread()");
            }
        }

        class Cheese {
            Cheese() {
                System.out.println("Cheese()");
            }
        }

        class Lettuce {
            Lettuce() {
                System.out.println("Lettuce()");
            }
        }

        class Lunch extends Meal {
            Lunch() {
                System.out.println("Lunch()");
            }
        }

        class PortableLunch extends Lunch {
            PortableLunch() {
                System.out.println("PortableLunch()");
            }
        }

        // 最下层,第二层,现在的层
        PortableLunch p = new PortableLunch();
        System.out.println("end!");
    }


    // 多态测试
    static void test3() {
        class Instrument {
            void play(Note n) {
                System.out.println("Instrument.play() " + n);
            }

            String what() {
                return "Instrument";
            }

            void adjust() {
                System.out.println("Adjusting Instrument");
            }
        }

        class Wind extends Instrument {
            void play(Note n) {
                System.out.println("Wind.play() " + n);
            }

            String what() {
                return "Wind";
            }

            void adjust() {
                System.out.println("Adjusting Wind");
            }
        }

        class Percussion extends Instrument {
            void play(Note n) {
                System.out.println("Percussion.play() " + n);
            }

            String what() {
                return "Percussion";
            }

            void adjust() {
                System.out.println("Adjusting Percussion");
            }
        }

        class Stringed extends Instrument {
            void play(Note n) {
                System.out.println("Stringed.play() " + n);
            }

            String what() {
                return "Stringed";
            }

            void adjust() {
                System.out.println("Adjusting Stringed");
            }
        }

        class Brass extends Wind {
            void play(Note n) {
                System.out.println("Brass.play() " + n);
            }

            void adjust() {
                System.out.println("Adjusting Brass");
            }
        }

        class Woodwind extends Wind {
            void play(Note n) {
                System.out.println("Woodwind.play() " + n);
            }

            String what() {
                return "Woodwind";
            }
        }

        Instrument[] orchestra = {
                new Wind(),
                new Percussion(),
                new Stringed(),
                new Brass(),
                new Woodwind()
        };

        for (Instrument i : orchestra)
            i.play(Note.MIDDLE_C);
    }

    // 画画接口
    interface IDraw
    {
        int VALUE = 5; // static & final
        void play(Note n); // Automatically public
        void draw();
    }
    // 游泳接口
    interface ISwim
    {
        void swim();
    }

    // 接口测试
    static void test4()
    {
        // 这些接口都是要实现,这里是两个接口
        class John implements IDraw,ISwim
        {
            public String toString() { return "Jonh class"; }
            public void play(Note n)
            {
                System.out.println(this + ".play() " + n);
            }
            public void draw()
            {
                System.out.println(this + ".draw()");
            }
            public void swim()
            {
                System.out.println(this+"--swim");
            }
        }

        // 每个类的接口方法都要实现,这里是两个接口
        class Nick implements IDraw,ISwim
        {
            public String toString() { return "Nick class"; }
            public void play(Note n)
            {
                System.out.println(this + ".play() " + n);
            }

            public void draw()
            {
                System.out.println(this + ".draw()");
            }
            public void swim()
            {
                System.out.println(this + "--swim");
            }
        }

        John j = new John();
        Nick c = new Nick();

        j.draw();
        j.swim();
        System.out.println( "string--:" + j );
        c.draw();
        c.swim();
        System.out.println( "string--:" + c );
    }


    public static void main(String[] args)
    {
        common_test();

        test();
        test2();
        test3();
        test4();

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值