复习java_2

来源:互联网 发布:安卓 软件灰度 编辑:程序博客网 时间:2024/05/22 03:34
package com.company;public class Arraytest{    public static void main(String args[]) {        char str1[] = {'j', 'a', 'v', 'a'};        char str2[] = new char[4];        //str2=str1;        System.arraycopy(str1, 0, str2, 0, str1.length);        System.out.print("str1:");        for (int i = 0; i < str1.length; i++) {            System.out.print(str1[i]);        }        System.out.println("");        System.out.print("str2:");        for (int i = 0; i < str2.length; i++) {            System.out.print(str2[i]);        }        System.out.println("");        str2[2]='u';        System.out.print("str1:");        for(int i=0 ;i<str1.length;i++){            System.out.print(str1[i]);        }        System.out.println("");        System.out.print("str2:");        for(int i=0 ;i<str2.length;i++){            System.out.print(str2[i]);        }        System.out.println("");    }}
package com.company;/** * Created by henson on 17-8-28. */public class ConstructCallThisAndSuper {    public static void main(String[] args){        Person4 p=new Graduate();    }}class Person4{    String name;    int age;    Person4(){}    Person4(String name,int age){        this.name=name;        this.age=age;        System.out.println("In Person4(String,int)");    }}class Student4 extends Person4{    String school;    Student4(){        //this(null,0);        this.school=school;        System.out.println("In Student()");    }    Student4(String name,int age,String school){        super(name,age);        this.school=school;        System.out.println("In Student(String,int,String)");    }}class Graduate extends Student4{    Graduate(){        System.out.println("Graduate@@@");    }}
package com.company;/** * Created by henson on 17-8-28. */public class ConstuctIn {    public static void main(String[] args){        Person5 p=new Graduate1();        Person5 p5=new Student5("LM",19,"THU");    }}class Person5{    String name="未命名";    int age=-1;    Person5(){}    Person5(String name,int age){        this.name=name;        this.age=age;        System.out.println("In Person4(String,int)");        sayHello();    }    void sayHello(){        System.out.println("Hello!!!我是"+name+", 我的年龄"+age);    }}class Student5 extends Person5{    String school="未定学校";    Student5(){        //this(null,0);        this.school=school;        System.out.println("In Student()");    }    Student5(String name,int age,String school){        super(name,age);        this.school=school;        System.out.println("In Student(String,int,String)");    }    void sayHello(){        System.out.println("我是学生"+name+",年龄为"+age+",学校在"+school);    }}class Graduate1 extends Student5{    Graduate1(){        System.out.println("Graduate@@@");    }}
package com.company;import java.io.FileInputStream;import java.io.IOException;/** * Created by henson on 17-8-28. */public class Excep {    public void main(String[] args) {        try {            FileInputStream in = new FileInputStream("myfile.txt");            int b;            b = in.read();            while (b != -1) {                System.out.println((char) b);                b = in.read();            }        } catch (IOException E) {            System.out.print(E);        } finally {            System.out.println("The end ");        }    }}
package com.company;import java.io.FileInputStream;import java.io.*;/** * Created by henson on 17-8-28. */public class ExeceptionIndexOutOf {    public static void main(String[] args){        String friends[]={"lisa","bily","kessy"};        try {            for (int i=0;i<5;i++)                System.out.println(friends[i]);        }catch (java.lang.ArrayIndexOutOfBoundsException e){            System.out.println("index error");        }        System.out.println("\nthis is the end");    }}
package com.company;/** * Created by henson on 17-8-28. */public class MyDate {    int year,month,day;    public MyDate(int y,int m,int d){        year=y;        month=m;        day=d;    }}package com.company;/** * Created by henson on 17-8-28. */public class MyOkDate extends MyDate {    public MyOkDate(int y,int m,int d){        super(y,m,d);    }    public String toString(){        return year+"-"+month+"-"+day;    }    public static void main(String[] args){        MyDate m1=new MyDate(24,3,2001);        MyDate m2=new MyOkDate(2010,12,12);        System.out.println(m1);        System.out.println(m2);    }}
package com.company;/** * Created by henson on 17-8-28. */interface Runner{public void run();}interface Swim{public void swimming();}abstract class Animal{abstract public  void eat();}class Person3 extends Animal implements Runner,Swim{    public void run(){System.out.println("run");}    public void swimming(){System.out.println("swimming");}    public void eat(){System.out.println("eat");}}public class TestInterface {    public void m1(Runner r){r.run();}    public void m2(Swim s){s.swimming();}    public void m3(Animal a){a.eat();}    public static void main(String args[]){        TestInterface t= new TestInterface();        Person3 p3 =new Person3();        t.m1(p3);        t.m2(p3);        t.m3(p3);    }}

让我对java有了新的认识

原创粉丝点击