您的位置:首页 > 其它

第二周第二天(abstract;匿名内部类/单例设计模式/不用for循环求11000的值/finalstatic/interface 接口/instanceof;interface)

2015-07-21 18:38 351 查看

目录

单例设计模式

不用for循环求1+…+1000的值

final,static的用法举例

interface 接口的用法举例

instanceof;interface;简单工厂设计模式

abstract;匿名内部类

单例设计模式

[code]package com.day2_2015_7_21;
//单例设计模式
public class Student1 {
    private static Student1 instance;//创建静态对象
    private Student1(){//私有化构造器
    }
    public static Student1 getInstance(){//创建静态得到对象的方法
    if(instance==null){//在静态方法中加条件语句
        instance=new Student1();
    }
      return instance;
}
}
package com.day2_2015_7_21;
public class test2 {
    public static void main(String[] args) {
    Student1 zhangsan=Student1.getInstance();
    Student1 lisi=Student1.getInstance();
    System.out.println(zhangsan);
    System.out.println(lisi);
}
}


不用for循环求1+…+1000的值

[code]package com.day2_2015_7_21;
public class Add {
private int i=1;
public int sum=0;
public void add(){
    if(i<1001){
    sum+=i;
    i++;
    add();
    }
}
}
package com.day2_2015_7_21;
public class Text3 {
public static void main(String[] args) {
    Add a=new Add();
    a.add();
    System.out.println(a.sum);
}
}


final,static的用法举例

[code]package com.day2_2015_7_21;
public final class Person {//final 修饰类,类就不能被继承
      public static int eyes=2;
      public static final  int NOSE_NUM=1;//final 修饰变量这个变量就不能再被修改
      public int age;
      public String name;
      public final void speak(){//final 修饰方法方法不允许被重写
          System.out.println("我们说中国话");
      }
      public void speak1(){//fianl 修饰变量 如果是全局变量必须立即复制,
          //如果是局部变量可以先声明再赋值
          final int i;
          i=12;
          System.out.println("我们说普通话");
      }
      public static void run(){
          System.out.println("我们会用脚跑");
      }
      public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
  }
package com.day2_2015_7_21;
import static com.day2_2015_7_21.Person.NOSE_NUM;
//静态导入
public class A {
    public static void main(String[] args) {
        Person zhangsan =new Person();
        Person.run();
        System.out.println("眼睛个数"+Person.eyes);
        System.out.println(NOSE_NUM);
    }
}


interface 接口的用法举例

[code]package com.day2_2015_7_21;
public interface Fly {

}
package com.day2_2015_7_21;
public class AirVehicle implements Fly {
    public void fly(){
        System.out.println("我会飞");
    }
}
package com.day2_2015_7_21;
public interface Load {

}
package com.day2_2015_7_21;
public class Plane extends AirVehicle implements Load{
    public void load(){
        System.out.println("我能载货");
    }
public static void main(String[] args) {
    Plane s=new Plane();
    s.fly();
    s.load();
}
}


instanceof;interface;简单工厂设计模式

[code]package com.day2_2015_7_21;
public interface Ink36 {
      public String getColor();
}
package com.day2_2015_7_21;
public interface Paper {
    public String getSize();
}
package com.day2_2015_7_21;
public interface Print {
    public void print(Ink36 ink,Paper paper);   
}
package com.day2_2015_7_21;
public class BeijingInk33 {
    public String getInk(){
        return("北京黑色");
    }
}
package com.day2_2015_7_21;
public class ShanghaiInk implements Ink36 {
    public String getColor(){
        return("上海红色");
    }
}
package com.day2_2015_7_21;
public class ChenguangPaper34 implements Paper {
    public String getSize(){
        return("晨光A4");
    }
}
package com.day2_2015_7_21;
public class OtherPaper implements Paper{
    public String getSize(){
        return("其他B2");
    }
}
package com.day2_2015_7_21;
public class HHPrint35 implements Print{
         @Override
    public void print(Ink36 a, Paper b){
        System.out.println("我使用的墨盒颜色是"+a.getColor()+"我使用的纸张大小"+b.getSize());
    }
}
package com.day2_2015_7_21;
public class ApplePrint32 implements Print{
    public void print(Ink36 a, Paper b){
        System.out.println("我使用的墨盒颜色是"+a.getColor()+"我使用的纸张大小"+b.getSize());
}
}
package com.day2_2015_7_21;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class Person1 {
public Print creatPrint(){
    //简单工厂设计模式
    Properties properties=new Properties();
    String s="";
    try{
        properties.load(new FileInputStream("config.properties"));
        s=properties.getProperty("print");
        System.out.println(s);
    }catch(IOException e){
        e.printStackTrace();
    }
    Print print=null;
    if(s.equals("hpl")){
        print=new HHPrint35();
    }else if(s.equals("apple")){
        print=new ApplePrint32();
    }
    //Print print=new HHPrint();
    return print;
}
public Paper creatPaper(){
    Paper paper=new OtherPaper();
    return paper;
}
public Ink36 creatInk(){
    Ink36 ink=new ShanghaiInk();
    return ink;
}
package com.day2_2015_7_21;
import com.day2_2015_7_21.Person1.Student;
public class Test1 {
public static void main(String[] args) {//这里写了三种方法第一种 print.print(ink,paper);第二种把第一种用到的类似Paper paper=person.creatPaper();的方法单独写了方式例如Print print =new HHPrint();用person.Print();输出第三种是用简单工厂设计模式
    Person1 person =new Person1();
    Paper paper=person.creatPaper();
//  Print print=person.creatPrint();
//  Ink ink=person.creatInk();
//  Print print =new HHPrint();
//  Paper paper=new ChenguangPaper();
//  Ink ink=new ShanghaiInk();
//  print.print(ink,paper); 
    person.Print();//注意不能写成Print print=person.Print();
    if(paper instanceof ChenguangPaper34){//对象instanceof 类/接口 判断是否是它的对象
        //该运算符用来判断一个对象是否属于一个类或者实现了一个接口,结果为ture或者false。
        System.out.println("我用的是晨光的纸");
    }else{
        System.out.println("我用的不是晨光的纸");
    }
 }
}
//在简单工厂设计模式下需要建立一个file,命名为config.properties
  print=hpl


abstract;匿名内部类

[code]package com.day2_2015_7_21;
public  abstract class Student {
    public abstract void readBook();//抽象的方法没有体
}
package com.day2_2015_7_21;
public class Schoolboy extends Student {
   public void readBook(){
       System.out.println("语文,数学,自然,社会");
   }
}
package com.day2_2015_7_21;
public class test {
    public static void main(String[] args) {
        Schoolboy boy =new Schoolboy();
        System.out.println(boy);
        boy.readBook();
        Student s=new Schoolboy();
        //匿名内部类(一般只能使用一个此类对象的时候使用
        //相当于创建了一个类继承了student类,
        //然后用此类构建一个对象)
        Student li=new Student(){
            public void readBook(){
            }
        };
        System.out.println(s);
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: