static静态
是类共同拥有的属性 public static int eyes=2;
是类共同的方法
静态的变量和方法可以通过“类名.”调用
Person.eyes Person.run()
static用法
可以调用其他静态方法
不能使用this、super关键字
final
public final class Person{}
用final修饰的类,此类不能被继承
用final 修饰方法,此方法不能重写
用final修饰变量,则变量值不允许修改
用final修饰全局变量要立刻赋值,修饰局部变量要可以先声明再赋值
public static final int EYE_NUM=2; 是常量
导入包
静态导入:import static com.baidu.test.Person.EYE_NUM;
Integer
parseInt 把字符串造型成int类型
package com.baidu.test3;
public class Test {
public static void main (String []args){
String s1="13";
String s2="14";
float f=20.15f;
int i=Integer.parseInt(s1);
int j=Integer.parseInt(s2);
System.out.println(s1+s2);//1314
System.out.println(i+j);//27
System.out.println(Math.floor(f));//20.0
System.out.println(Math.ceil(f));//21.0
System.out.println(Math.rint(f));//20.0
}
}
抽象类
public abstract class Student {
public abstract void readBook();//抽象方法
}
抽象方法没有方法体
抽象方法必须在抽象类中
抽象方法必须在子类中被实现
public class Schoolboy extends Student{
public void readBook(){
System.out.println("语文数学");
}
}
public class Test2 {
public static void main(String[] args) {
Schoolboy boy=new Schoolboy();//第一种
boy.readBook();//第一种打印方法
Student s=new Schoolboy();//第二种
s.readBook();//第二种打印方法
Student lisi=new Student(){
public void readBook(){
System.out.println("语文");//打印语文
}
};
lisi.readBook();
}
}
匿名内部类,一般在只使用一个此类对象的时候使用,相当于创建了一个类继承Student 类,然后用此类构建了一个对象
接口(interface):可以被多继承
public interface Fly{//不能有变量,只允许有常量和抽象方法
public void fly();//无方法体
}
package com.java.test;
public interface Paper {
public String getPaperSize();
}
package com.java.test;
public interface Ink {
public String getInkColor();
}
package com.java.test;
public interface Print {
public void print(Ink ink,Paper paper);
}
package com.java.test;
public class BeijingPaper implements Paper {
@Override
public String getPaperSize() {
return "北京A4";
}
}
package com.java.test;
public class ShanghaiPaper implements Paper {
@Override
public String getPaperSize() {
return "上海B5";
}
}
package com.java.test;
public class BeijingInk implements Ink{
@Override
public String getInkColor() {
return "北京红色";
}
}
package com.java.test;
public class ShanghaiInk implements Ink{
@Override
public String getInkColor() {
return "上海黑色";
}
}
package com.java.test;
public class HPPrint implements Print {
@Override
public void print(Ink ink, Paper paper) {
System.out.println("我使用的墨盒是:"+ink.getInkColor()+" 我使用的纸是:"+paper.getPaperSize());
}
}
package com.java.test;
public class OtherPrint implements Print{
@Override
public void print(Ink ink, Paper paper) {
System.out.println("使用的纸是:"+paper.getPaperSize()+" 使用的墨盒是:"+ink.getInkColor());
}
}
package com.java.test;
public class Factory {//方便改动
public static Paper creatPaper(){
return new BeijingPaper();//北京A4
}
public static Ink creatInk(){
return new ShanghaiInk();//上海黑色
}
public static Print creatPrint(){
return new HPPrint();//HPPrint
}
}
package com.java.test;
public class Test {
public static void main(String[] args) {
Paper paper=Factory.creatPaper();
Ink ink=Factory.creatInk();
Print print=Factory.creatPrint();
print.print(ink, paper);//我使用的墨盒是:上海黑色 我使用的纸是:北京A4
}
}
多态
使用父类的引用指向子类的对象
同一个引用,经过操作,产生不同的结果
内部类
public class Person{
public class Student{
}
}
调用:Student zhangsan=new Person().new Student();
设计模式
单例(单子)设计模式
public class Student{
//1.创建静态对象
//2.创建私有构造器
//3.创建静态得到对象的方法
//4.在静态方法中加条件语句
}
package com.baidu.test3;
public class Student {
private static Student instance;//创建静态对象
private Student(){ //创建私有构造器
}
public static Student getInstance(){//创建静态方法
if (instance==null){ //在静态方法中加入条件语句
instance=new Student();
}
return instance;
}
}
package com.baidu.test3;
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);
}
}
工厂设计模式(创建一个文件,命名config.properties)
package com.baidu.test1;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class Person {
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 ex){
System.out.println(ex);
}
Print print =null;
if(s.equals("apple")){
print=new ApplePrint();
}else if(s.equals("hp")){
print=new HPPrint();
}
return print;
}
public Paper creatPaper(){
Paper paper=new ChenGPaper();
return paper;
}
public Ink creatInk(){
Ink ink =new ShanghaiInk();
return ink;
}
}
package com.baidu.test1;
import com.baidu.test1.Person.Student;
public class Test {
public static void main(String[] args) {
Person person =new Person();
Ink ink=person.creatInk();
Print print=person.creatPrint();
Paper paper=person.creatPaper();
print. print(ink,paper);
if(print instanceof HPPrint){
System.out.println("使用了惠普Print"); //对象+instanceof+类/接口:判断对象是否属于类或实现了一个接口
}else{
System.out.println("使用了ApplePrint");
}
}
}