第二周第二天(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); } }
相关文章推荐
- hdu 5289 Assignment 2015 Multi-University Training Contest 1
- Android Wear(手表)开发 - 学习指南
- ubutun14.04编译hadoop-2.6.0源码,并生成eclipse项目
- 7.第三单元任务四实训:方法setId能为类的属性:id(身份证号码)设置值,当给定的值长度为18时,赋值给id,当值长度不是18时,抛出IllegalArgumentException异常。在mai
- UIKit框架-11.UITableViewCell概述
- 几点建议,让Redis在你的系统中发挥更大作用
- 海量数据查询代码优化--转载
- jQuery Attributes vs. Properties
- 令人失望的xfs文件系统
- Android--单元测试案例
- UIKit框架-10.UITableView概述
- Git学习笔记之<Git基础>
- OJ第六批——Problem C: A代码完善--哺乳动物
- 嘻唰唰第六批B改错题--植物与花
- 优先队列
- windows程序设计——飞机大战笔记(调用登陆数据库)
- OJ第六批——Problem B: A改错题--植物与花
- Play! 之各种Render
- HDU 5299 Circles Game
- cocos2dx自学之使用box2d物理世界