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();

    }
}

 

你可能感兴趣的:(java)