您的位置:首页 > 编程语言 > Java开发

JAVA学习第六天

2015-07-22 08:06 513 查看
static final

采用递归的算法 从1加到1000 求和 不使用循环语句

单例设计模式(单子设计模式)单例

简单的工厂设计模式

匿名(没有子类)内部类

parseInt abs floor ceil rint的用法

static final

static描述的是一类事物共有的特性供调用

只用通过静态的方法 定义的变量和方法才可以通过类名.变量方法 调用

static 静态方法中不可以使用非静态的方法 不可以访问非静态的数据 不能调用super this关键字 (涉及到对象的 特有的static 不可以使用)

final修饰类 此类不能被继承

final修饰method不允许被重写

final修饰变量值不允许被修改

final 修饰全局变量要立刻赋值,修饰局部变量可以声明赋值也可以先声明后赋值

Person

package com.donghe.svn;
public class  Person {//final  要放在class前面//  final最终的不可修改的
public final static int EYES_NUM=2;//只有通过static的变量和方法可以通过类名.方法/常量调用   类中对象共有的属性   常量字母全部大写多个单词用_隔开
private static  int age=8;//对象特有的一些属性 可能不一样
private String name;
public static void run(){

System.out.println(Person.age);

System.out.println("用脚走路");
}
public void eat(){
final int abc;//变量是方法体中定义的称为临时变量  属性是在类中定义的     访问修饰符只修饰属性和方法 不修饰变量,   方法中的临时变量在调用完成后就不存在了,不需要修饰定义
abc=2;           //final定义局部变量可以声明赋值  也可以先声明后赋值
}

}


Person_Test

package com.donghe.svn;

public class Person_Test  extends Person {

public static void main(String[] args) {
// TODO Auto-generated method stub
Person  person=new Person();
System.out.println(person.EYES_NUM);
//          Person.eyes=3;
Person.run();

}

}


常量在不同包中调用

package com.donghe.svn1;

import com.donghe.svn0.Person;
import   static  com.donghe.svn.Person.EYES_NUM;  //导入的是常量  未导入EYES_NUM与Person的关系

public class Test2 {
public static void main(String[] args) {
Person  person=new  Person();//通过导入第二个包实现的
System.out.println(EYES_NUM);
//System.out.println(person.EYES_NUM);//所以不可以使用对象person去调用
}

}


采用递归的算法 从1加到1000 求和 不使用循环语句

package com.donghe.svn1;
public class Add {
private  int  i=1;
private  int sum=0;//私有的 sum  可以通过getSum方法获得
public void add(){

if(i<1001){

sum+=i;
i++;//i++的位置
add();//方法体内再次调用add()方法
}

}
public  int  getSum(){

return sum;
}

}


Test代码

package com.donghe.svn1;

public class Add_Test {
public static void main(String[] args) {
Add  a=new Add();
a.add();//很重要     需要实现add();方法 才可以给sum赋加值
System.out.println(a.getSum());
}

}


单例设计模式(单子设计模式)单例

类只产生一个对象

package com.donghe.svn1;

public class Student {
public static  Student instance;//3.创建一个 静态的对象    因为静态方法不可以调用非静态的方法和数据//Student为参数类型(数据类型)
private Student(){//1.private隐藏构造器

}
public  static Student getInstance(){//2.创建静态得到一个对象的方法   采用静态原因:静态的是其他类可以通过类名加'.'访问

if(instance==null){//instance:实例         下一次instance不为null    只得到一个学生
//在静态语句中加入条件语句
instance=new Student();
}

return   instance;

}

}


Test

package com.donghe.svn1;

public class Test1 {
public static void main(String[] args) {

Student zhangsan =Student.getInstance();
Student  lisi=Student.getInstance();
System.out.println(zhangsan);
System.out.println(lisi);
}

}


简单的工厂设计模式

Print接口

package com.donghe.svn0;

public interface Print {//Print接口
public void print(Ink  ink,Paper paper);//Ink  ink,Paper paper 传入两个参数    一个打印的方法无需返回值

}


Paper接口

package com.donghe.svn0;

public interface Paper {
public String  getSize();   //纸张的大小  //需要返回值String

}


Ink接口

package com.donghe.svn0;

public interface Ink {
public String getColor();//墨盒的颜色   需要返回值String
}


BeijingInk

package com.donghe.svn0;

public class BeijingInk implements  Ink{

public String getColor() {
// TODO Auto-generated method stub
return "北京红色";//输入需要返回的字符串
}

}


SahnghaiInk

package com.donghe.svn0;

public class ShanghaiInk implements  Ink{

public String getColor() {
// TODO Auto-generated method stub
return "上海黑色";//输入需要返回的字符串
}

}


CehnguangPaper

package com.donghe.svn0;

public class ChenGuangPaper implements Paper{

public String getSize() {
// TODO Auto-generated method stub
return "ChenGuangA4";   //输入需要返回的字符串
}

}


OtherPaper

package com.donghe.svn0;

public class OtherPaper implements  Paper{

public String getSize() {
// TODO Auto-generated method stub
return "其他纸张";//输入需要返回的字符串
}

}


HPPrint

package com.donghe.svn0;

public class HPPrint implements  Print{

//方法的重写
public void print(Ink ink, Paper paper) {
// TODO Auto-generated method stub
System.out.println("惠普打印机"+"我使用的纸张是"+paper.getSize()+"\n"+"墨盒是"+ink.getColor());
}
//方法的重载
public void print(){

}

}


ApplePrint

package com.donghe.svn0;

public class ApplePrint implements Print{

public void print(Ink ink, Paper paper) {
// TODO Auto-generated method stub
System.out.println("苹果打印机"+"\t"+paper.getSize()+"\n"+"\t"+ink.getColor());
}

}


Person(执行打印的人 拥有方法的人)

//简单的工厂设计模式
package com.donghe.svn0;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class Person {
public Print creatPrint() {// 需要返回类型是打印机 Print是接口属于抽象类

//首先在建立一个文档   该文档放的位置与src文件夹位置平齐      //文件名为config.properties(可变    配置属性)
Properties properties = new Properties();//导包    建立properties 对象  重要
String s = "";
try {
properties.load(new FileInputStream("config.properties"));   //load      FileInputStream文件流
s = properties.getProperty("print");   //getProperty:用指定的键在此属性列表中搜索属性
System.out.println(s);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("ddddddddd");//sysout    通过打印找错误
Print print = null;     //定义指针为空
if (s.equals("hpp")) {     //config.properties里面也不要写;号
print = new HPPrint();

} else if (s.equals("apple")) {
print = new ApplePrint();

}

return print;  //需要创建print 变量
}

public Ink creatInk() {// 需要返回类型是Ink
//      Ink ink = new BeijingInk();
//      return ink;

Properties properties = new Properties();//导包    建立properties 对象  重要
String s = "";
try {
properties.load(new FileInputStream("config.properties"));   //load      FileInputStream文件流
s = properties.getProperty("ink");   //getProperty:用指定的键在此属性列表中搜索属性
System.out.println(s);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Ink ink = null;     //定义指针为空
if (s.equals("beijingink")) {     //config.properties里面也不要写;号
ink =  new BeijingInk();

} else if (s.equals("shanghaiink")) {
ink =  new ShanghaiInk();

}

return  ink;  //需要创建print 变量
}

public Paper creatPaper() {// 需要返回类型是Paper
//      Paper paper = new ChenGuangPaper();
//      return paper;
Properties properties = new Properties();//导包    建立properties 对象  重要
String s = "";
try {
properties.load(new FileInputStream("config.properties"));   //load      FileInputStream文件流
s = properties.getProperty("paper");   //getProperty:用指定的键在此属性列表中搜索属性
System.out.println(s);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Paper paper = null;     //定义指针为空
if (s.equals("chenguanga4")) {     //config.properties里面也不要写;号
paper =  new ChenGuangPaper();

} else if (s.equals("otherb2")) {
paper =  new OtherPaper();

}

return  paper;  //需要创建print 变量
}

//封装  将创建纸 Ink  Paper 封装起来
public void print() {
Print prints = creatPrint();
Paper paper = creatPaper();
Ink ink = creatInk();
prints.print(ink, paper);

//调用内部类的变量
Student zhangsan =new Student();
System.out.println(zhangsan.age);
}

// 内部类
class Student {
static final int age = 15;//常量
}
}


Print_Paper_Ink_Test

package com.donghe.svn0;

import com.donghe.svn0.Person.Student;

public class Print_Paper_Ink_Test {

public static void main(String[] args) {
// TODO Auto-generated method stub
Person person =new Person();
person.print();//采用封装打印

//调用  person中的方法打印

//            Print print=person.creatPrint();//我需要一台打印机你给了我一台惠普打印机
//           Ink  ink=person.creatInk();
//           Paper paper=person.creatPaper();
//            print.print(ink, paper);
//面向接口的程序设计

//            if(print  instanceof HPPrint){
//              ((HPPrint) print).print();
//              System.out.println("惠普的打印机");
//            }else{
//              System.out.println("不是惠普的打印机");
//            }

//创建内部类的对象
Student zhangsan=new Person().new  Student();//方法比较特殊
System.out.println(zhangsan.age);

}

}


匿名(没有子类)内部类

Android里回调使用的比较多

//匿名(没有类名)内部类  一般只在使用一个此类对象(学生的)时候使用
//相当于一个子类继承了Student类        然后用此子类构造了一个对象
//Schoolboy sb = new Schoolboy();
//Student s = (Student)sb;
Student  student2=new   Student(){

@Override
public void readBook() {
// TODO Auto-generated method stub

}

};
student2.readBook();


抽象类 抽象方法

Student

package com.donghe.svn;

public abstract class Student {//抽象的类     抽象类除了有抽象方法外还有非抽象的方法   而抽象方法必须放在抽象类里面
private int age;

public int getAge(){//抽象类中可以包含非抽象的方法
return  age;
}
public abstract void readBook();//抽象方法没有方法体   返回值类型前面加abstract

}


Schoolboy

package com.donghe.svn;

public class Schoolboy  extends Student {

@Override//抽象方法必须在子类中实现    除非子类是抽象类也就是说要么Schoolboy也为抽象类
public void readBook() {
// TODO Auto-generated method stub
System.out.println("数学语文自然社会");
}

}


StudentSchoolboyTest

package com.donghe.svn;

public class StudentSchoolboyTest {
public static void main(String[] args) {
Schoolboy  boy=new Schoolboy();//   学生为抽象类所以学生的实例创建不了  只能创建更具体的小学生
boy.readBook();
Student     student=new Schoolboy();//我要一个学生  你给了我一个小学生  可以的小学生是学生的子类
}


接口Interface(面向接口的程序设计)

接口相当于某种能力。在创建一个类implements 一个接口的时候,只是说有这种能力,但是具体怎么做还需要在这个类中进行重写。

(类似于只有抽象方法的抽象类 不考虑实现只考虑方法 )

把类和方法分离开

1.没有方法体 不可有变量

2.只允许有抽象方法(可以含参数) 和常量(static final)

常量一般格式为

public static final int AGE=3;

接口特点

1.接口不可以被实例化

2.实现类必须实现接口所有的方法

3.实现类可以实现多个接口 implements

4.接口中的变量都是常量

接口中的成员和方法都必须是public类型的

有get/set不要使用void 需要有返回值

封装 继承 多态



parseInt abs floor ceil rint的用法

PersonStudent_Test

package com.donghe.test;

public class PersonStudent_Test {

public static void main(String[] args) {
// TODO Auto-generated method stub
Student  student =new  Student();
System.out.println();

String s="215";
String s2="326";
int  a=new  Integer(Integer.parseInt(s));//parseInt的用法
int  b=new  Integer(Integer.parseInt(s2));
int c=-12;
double  d=215.52;
double  f=215.42;

System.out.println(s+s2);
System.out.println(a+b);//parseInt的使用让字符串造型为Int进行相加
System.out.println(Math.abs(c));//abs绝对值
System.out.println(Math.floor(d));//floor215.0
System.out.println(Math.ceil(f));//ceil216.0
System.out.println(Math.rint(f));//rint四舍五入//215.0
System.out.println(Math.rint(d));//216.0

}

}


Person父类

package com.donghe.test;

public class Person{
public Person(){
System.out.println("无参构造器");
}
}


Student子类

package com.donghe.test;

public class Student  extends  Person {
public Student(){
super();//可以省略不写 不写的时候不要忘了
System.out.println("wucangouzaoqi");
}
}


copy

类的定义

public class 类名{
访问修饰符 类型 属性名      //成员变量,也叫全局变量  成员属性
//成员方法
访问修饰符 返回值类型 方法名(参数类型 参数){
类型 属性名  //局部变量   //临时变量
*方法体*
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: