1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Java方法工厂 接口 常用日期类Date面向对象 匿名内部类局部类

Java方法工厂 接口 常用日期类Date面向对象 匿名内部类局部类

时间:2022-10-07 20:56:50

相关推荐

Java方法工厂 接口 常用日期类Date面向对象 匿名内部类局部类

抽象

1抽象类不能生成对象

2类中包含抽象函数,这个类必须声明为抽象类

3抽象类不一定有抽象方法。有抽象方法必须是抽象类

4若全是抽象方法,则为接口

接口

比如interface action{void fly():}这里我定义个接口叫动作执行,里面有个飞的方法,至于怎么飞没人知道,但是只要你实现这个接口就必须得飞,哪怕是鸵鸟...现在有一个类实现了class bird implements action{public void fly(){//我用翅膀飞}}又一个类实现了, 鸵鸟大哥也要飞class ostrich implements action{public void fly(){//我跑的快,勉强也算飞,你管我怎么飞,反正我就fly}}这里就比较有意思了,可以理解这两个鸟肩膀肋下都有两个洞(接口),给个翅膀就能飞,但是行为不一样,一个煽动翅膀,一个猛跑....在软件中可以写这样的方法public void test(action myStyle){}未来调用这个方法时可以写多个,因为方法参数是接口类型,那么传递进去的参数必须是实现此接口类的一个对象比如test(new bird());test(new ostrich());根据传递实现类的对象,执行的行为却不一样,但是test方法却可以体现写完不用动了,以后想扩展其他fly行为,我可以继续写个类实现action接口。这叫向后兼容一个类实现了接口就必须实现其里面所有方法,除非这个类是抽象类,就是用abstract修饰的。接口之间可以继承这里要区分继承和实现,关键字也不一样extends继承, implements实现java中不允许一个类继承多个类,但是可以实现多个接口

方法工长

为什么工厂模式是如此常用?因为工厂模式就相当于创建实例对象的new,我们经常要根据类Class生成实例对象,如A a=new A() 工厂模式也是用来创建实例对象的,所以以后new时就要多个心眼,是否可以考虑实用工厂模式,虽然这样做,可能多做一些工作,但会给你系统带来更大的可扩展性和尽量少的修改量。 我们以类Sample为例, 如果我们要创建Sample的实例对象: Sample sample=new Sample(); 可是,实际情况是,通常我们都要在创建sample实例时做点初始化的工作,比如赋值 查询数据库等。 首先,我们想到的是,可以使用Sample的构造函数,这样生成实例就写成: Sample sample=new Sample(参数); 但是,如果创建sample实例时所做的初始化工作不是象赋值这样简单的事,可能是很长一段代码,如果也写入构造函数中,那你的代码很难看了(就需要Refactor重整)。 为什么说代码很难看,初学者可能没有这种感觉,我们分析如下,初始化工作如果是很长一段代码,说明要做的工作很多,将很多工作装入一个方法中,相当于将很多鸡蛋放在一个篮子里,是很危险的,这也是有背于Java面向对象的原则,面向对象的封装(Encapsulation)和分派(Delegation)告诉我们,尽量将长的代码分派“切割”成每段,将每段再“封装”起来(减少段和段之间偶合联系性),这样,就会将风险分散,以后如果需要修改,只要更改每段,不会再发生牵一动百的事情。 在本例中,首先,我们需要将创建实例的工作与使用实例的工作分开, 也就是说,让创建实例所需要的大量初始化工作从Sample的构造函数中分离出去。 这时我们就需要Factory工厂模式来生成对象了,不能再用上面简单new Sample(参数)。还有,如果Sample有个继承如MySample, 按照面向接口编程,我们需要将Sample抽象成一个接口.现在Sample是接口,有两个子类MySample 和HisSample .我们要实例化他们时,如下: Sample mysample=new MySample(); Sample hissample=new HisSample(); 随着项目的深入,Sample可能还会"生出很多儿子出来", 那么我们要对这些儿子一个个实例化,更糟糕的是,可能还要对以前的代码进行修改:加入后来生出儿子的实例.这在传统程序中是无法避免的. 但如果你一开始就有意识使用了工厂模式,这些麻烦就没有了. 工厂方法 你会建立一个专门生产Sample实例的工厂: public class Factory{public static Sample creator(int which){ //getClass 产生Sample 一般可使用动态类装载装入类。 if (which==1) return new SampleA(); else if (which==2) return new SampleB(); } } 那么在你的程序中,如果要实例化Sample时.就使用 Sample sampleA=Factory.这样,在整个就不涉及到Sample的具

内部类

Pen pen=zhangsan.new Pen();

package com.内部类.android;public class Student {int age;String name;public void delay(){for (int i = 0; i <100; i++) {for (int j = 0; j <100; j++) {}}}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;System.out.println(name);}void studey (){System.out.println("学习");}//内部类class Pen{String name;public String getName() {return name;}public void setName(String name) {Pen.this.name = name;//Student.this.name=name;}void write(){//局部类class Book{}System.out.println("写东西");Book book=new Book();}}}

局部类

在方法中定义的类。之在方法中声明对象

void write(){//局部类class Book{}System.out.println("写东西");Book book=new Book();}}

匿名内部类

//zhangsan是student类子类的匿名类的一个对象可以复写方法Student zhangsan=new Student(){@Overridevoid studey() {// TODO Auto-generated method stubsuper.studey();}};

当只使用一次这个对象的时候

Date日期的类

//日期dateDate time=new Date();System.out.println(time.toString());s.delay();Date time1=new Date();System.out.println(time1.toString());System.out.println(time.before(time1));//日期CalendarCalendar c= Calendar.getInstance();System.out.println(c.get(Calendar.YEAR));System.out.println(c.get(Calendar.MONTH));System.out.println(c.get(Calendar.DAY_OF_MONTH));c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH)+2);System.out.println(c.get(Calendar.DAY_OF_MONTH)); //格式化输出//SimpleDateFormat XXXX年XX月XX日 XX:XX:XXSimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");String time3 =format.format(c.getTime());System.out.println(time3);//long类型的毫秒数从1970年0:0:0开始//parse解析08月03日 16:02:37 为秒String time4="08月03日 16:02:37";try {Date date=format.parse(time4);System.out.println(date.getTime());} catch (Exception e) {e.printStackTrace();}

正则表达式

//正则表达式//手机号 11位数字 开头13,14,15,17,18.跟9位数字Pattern p1 = pile("^1(3|4|5|7|8)\\d{9}$");Matcher m=p1.matcher("13568578987");boolean b=m.matches();System.out.println(b);//写身份证18位最后一位X或x//邮箱XXXX@|.cn|netPattern p2 = pile("^\\d{17}(X|x|\\d{1})$");Matcher m2=p2.matcher("13568578987111111x");boolean b2=m2.matches();System.out.println(b2);Pattern p3 = pile("^\\d{17}(.com|.cn|.net$");Matcher m3=p3.matcher("13568578987111111x");boolean b3=m3.matches();System.out.println(b3);}

SimpleDateFormat

异常

throws XXXException

try {

} catch (FileSource e) {

e.printStackTrace();

}//下面重写异常

package com.流.android;public class FileSource extends Exception{public FileSource(){super("不及格的异常");}@Overridepublic void printStackTrace() {System.out.println("成绩不理想");super.printStackTrace();}}---------------------------------------------------------package com.流.android;public class Student {int score;public int getScore() {return score;}public void setScore(int score)throws FileSource {if (score<60){throw new FileSource();}else if(score<90){System.out.println("另一个异常");}this.score = score;}}----------------------------------------------------------package com.流.android;public class Test1 {public static void main(String[] args) {Student s=new Student();try {s.setScore(59);} catch (FileSource e) {// TODO Auto-generated catch blocke.printStackTrace();}}}--------------------------------------------------------------com.流.android.FileSource: 不及格的异常at com.流.android.Student.setScore(Student.java:13)at com.流.android.Test1.main(Test1.java:8)成绩不理想

//以字节流读取存储防止编码方式不同显示乱码public static void main(String[] args) {FileInputStream fi=null;FileOutputStream fo=null;try {fi=new FileInputStream("C:/11.txt");fo=new FileOutputStream("C:/22.txt");byte []buffer=new byte[1024];int temp=fi.read(buffer, 0, buffer.length);fo.write(buffer, 0, temp);String s=new String(buffer);s=s.trim();System.out.println(s);} catch (Exception e) {System.out.println(e);}}

/**写文件 */try {File file=new File("f://22.txt");FileOutputStream fos;fos = new FileOutputStream(file);OutputStreamWriter osw=new OutputStreamWriter(fos);BufferedWriter bw=new BufferedWriter(osw);bw.write("1232easdasdsadase32ewedw");bw.flush();fos.close();osw.close();bw.close();}catch (FileNotFoundException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}---------------------------------------------------------------//读文件package com.流.android;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import org.omg.CORBA.portable.InputStream;public class Test2 {public static void main(String[] args) {File file=new File("d://11.txt");try {//FileInputStream fis=new FileInputStream(file);//读字节InputStreamReader reader=new InputStreamReader(fis);//读字符BufferedReader br=new BufferedReader(reader);//读行String line =br.readLine();while (line!=null){System.out.println(line);line=br.readLine();}br.close();reader.close();fis.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。