面向对象编程的三(四)大特征
封装,继承,多态(,抽象)

基础语法:

  • 数组(int[] a或 int a[])、字符串组(String []a或 String a[] )
  • 循环
   for(int i;i<n;i++){}
    int []x;
    for(int i:x)
    用i来迭代x
    while(num--){}
    do{}while(num--)
  • 选择
    if(){

    }else{

    }

    swich(score){

    case 1:....;break;

    case 2:....;break;
    }

  • 类中构造方法的原则是只实现自己的功能
  • String 是一个类,字符串常量池是堆中的一个存字符串值的一个集合,他的赋值方式有两种

    • String name = new String ("张三"); 创建两个对象字符串常量池和堆内存中;
    • String name = "张三";创建一个对象,推荐使用
  • 字符串存在于常量池中

类的操作

1.类是一个具有相同属性的集合,对象是其中的一的个例
类中包含属性和方法,公共的属性有对象调用

class Person{
    //属性的定义
    String name;
    int age;
    char sex;
    //方法的定义
    public void show(){
        System.out.println("我是"+name+",今年"+age+"岁了,"+"是一个"+sex+"人");
    }
}
public class Notes{
    public static void main(String []args){
        Person p = new Person();
        p.name = "张三";
        p.sex = '男';
        p.age = 10;
        p.show();
    }
}

2.封装性
常用:属性的封装

 class Person{
    //属性的定义
    private String name;
    private int age;
    private char sex;
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setAge(int age){
        this.age = age;
    }
    public int getAge(){
        return age;
    }
    public void setSex(char sex){
        this.sex = sex;
    }
    public char getSex(){
        return sex;
    }
    //方法的定义
    public void show(){
        System.out.println("我是"+name+",今年"+age+"岁了,"+"是一个"+sex+"人");
    }
}

public class Notes{
    public static void main(String []args){
        Person p = new Person();
        p.setName("张三");
        p.setSex('男');
        p.setAge(10);
        p.show();
    }
}

3.构造方法的重写
默认为无参函数
构造方法和类名相同,为大写开头
重写构造方法后自动覆盖掉无参,若想调用即需写出无参构造方法
构造方法可以写多个

class Person{
    //属性的定义
    private String name;
    private int age;
    private char sex;
    public Person(){}
    public Person(String name){
        this.name = name;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setAge(int age){
        this.age = age;
    }
    public int getAge(){
        return age;
    }
    public void setSex(char sex){
        this.sex = sex;
    }
    public char getSex(){
        return sex;
    }
    //方法的定义
    public void show(){
        System.out.println("我是"+name+",今年"+age+"岁了,"+"是一个"+sex+"人");
    }
}
public class Notes{
    public static void main(String []args){
        Person p = new Person();
        p.setName("张三");
        p.setSex('男');
        p.setAge(10);
        p.show();
        Person q = new Person("李四");
        q.setSex('女');
        q.setAge(10);
        q.show();
    }
}

4.方法的重载(overloading Method)
条件:方法名相同,参数不同

class Person{
    //属性的定义
    private String name;
    private int age;
    private char sex;
    public Person(){}
    public Person(String name){
        this.name = name;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setAge(int age){
        this.age = age;
    }
    public int getAge(){
        return age;
    }
    public void setSex(char sex){
        this.sex = sex;
    }
    public char getSex(){
        return sex;
    }
    //方法的定义
    public void show(){
        System.out.println("我是"+name+",今年"+age+"岁了,"+"是一个"+sex
        +"人,我的伴侣是李四");
    }
    public void show(String name){
        System.out.println("我是"+name+",今年"+age+"岁了,"
        +"是一个"+sex+"人,我的伴侣是"+name);
    }
}
public class Notes{
    public static void main(String []args){
        Person p = new Person();
        p.setName("张三");
        p.setSex('男');
        p.setAge(10);
        p.show();
        p.show("王五");
    }
}

5.匿名对象
1.没有名称 2.只能使用一次
3.直接在堆中开辟内存 4.使用后被回收

class Person{
    //属性的定义
    private String name = "张三";
    private int age = 10;
    private char sex = '男';
    public Person(){}
    public Person(String name){
        this.name = name;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setAge(int age){
        this.age = age;
    }
    public int getAge(){
        return age;
    }
    public void setSex(char sex){
        this.sex = sex;
    }
    public char getSex(){
        return sex;
    }
    //方法的定义
    public void show(){
        System.out.println("我是"+name+",今年"+age+"岁了,"+"是一个"+sex
        +"人,我的伴侣是李四");
    }
    public void show(String name){
        System.out.println("我是"+name+",今年"+age+"岁了,"
        +"是一个"+sex+"人,我的伴侣是"+name);
    }
}
public class Notes{
    public static void main(String []args){
        new Person().show();
    }
}

6.String类的编译期和运行期

public class Notes{
    public static void main(String []args){
        //情况一:true
        String a = "s1";
        String a1 = "s"+1;//两个常量连接
        System.out.println(a == a1);
        
        //情况二:false
        String b = "s1";
        int bb = 1;
        String b1 = "s"+bb;//因为此处的bb为变量
        System.out.println(b == b1);
        
        //情况三:true
        String c = "s1";
        final int cc = 1;//此处声明了一个常量
        String c1 = "s" + cc;//在这里cc代表了一个常数
        System.out.println(c == c1);

        //情况四:false
        String d = "s1";
        final int dd = getDD();//此处需要到运行期才可以确定
        String d1 = "s" + dd;
        System.out.println(d == d1);
    }
    public static int getDD(){
        return 1;
    }
}

7.String类的操作方法

//1.根据下标找字符

public class Notes{
    public static void main(String []args){
        String test = "i love java";
        char result = test.charAt(3);
        System.out.println("第4个字符是"+result);
    }
}

//2.字符串变字符数组

public class Notes{
    public static void main(String []args){
        String test = "i love java";
        char []result = test.toCharArray();
        for(char i:result)
            System.out.print(i+",");
    System.out.println();
    }
}

//3.字符串的截取

public class Notes{
    public static void main(String []args){
        String test = "i love java";
        String result = test.substring(6);
        System.out.println(result);
        result = test.substring(0,6);//包含起始位置不包含结束位置
        System.out.println(result);
    }
}

//4.字符串的拆分

public class Notes{
    public static void main(String []args){
        String test = "i love java";
        String []result = test.split(" ");
        for(String i:result)
            System.out.print(i+"|\t");
        System.out.println("\n==============");
        String result_1[] = test.split(" ",2);
        for(String i:result_1)
            System.out.print(i+"\t");
        System.out.println();
    }
}

//5.字符串的查找,替换,大小写转换,长度计算

public class Notes{
    public static void main(String []args){
        String test = " i love java ";
        
        Boolean b = test.contains("a");
        System.out.println(b);
        
        int index = test.indexOf("l");
        System.out.println(index);
        
        index = test.indexOf("java");//第一字母出现的位置,该单词不存在返回-1
        System.out.println(index);
        
        int index_1 = test.lastIndexOf("a");//从后向前查找
        System.out.println(index_1);
        
        String result = test.toUpperCase();
        System.out.println(result);
        
        result = result.toLowerCase();
        System.out.println(result);
        
        b = test.isEmpty();
        System.out.println(b);
        
        result = test.concat(" too!");        
        System.out.println(result);
        
        int cnt = test.length();
        System.out.println(cnt);
        
        result = test.trim();
        System.out.println(result);
        
        result = test.replace(" ","-");
        System.out.println(result);
        
    }
}

8.值传递与引用传递

//1.值传递,String也可以这样表示

public class Notes{
    public static void main(String []args){
        int b = 1;
        method(b);    
        System.out.println(b);
    }
    public static void method(int c){
            c = 2;
    }
}

//2.方法传递

public class Notes{
    public static void main(String []args){
        Cat b = new Cat();
        b.age = 12;
        method(b);    
        System.out.println(b.age);
    }
    public static void method(Cat c){
            c.age = 20;
    }
}
class Cat{
    int age = 10;
}

9.对象的一对一关系

public class Notes{
    public static void main(String [] args){
        Husband h = new Husband("张三",'男');
        Wife w = new Wife("李四",15);
        h.wife = w;
        w.husband = h;
        h.show();
        w.show();
        h.wife.show();
        w.husband.show();
        
    }
}

class Husband{
    String name;
    char sex;
    Wife wife;//关联是将对方的类作为属性导入,关联妻子类
    public Husband(){}
    public Husband(String name, char sex){
        this.name = name;
        this.sex = sex;
    }
    public void show(){
        System.out.println("我是"+name+",我的妻子是"+wife.name);
    }
}

class Wife{
    //实际操作中应该封装
    String name;
    int age;
    Husband husband;//关联是将对方的类作为属性导入,关联丈夫类
    public Wife(){}
    public Wife(String name, int age){
        this.name = name;
        this.age = age;
    }
    public void show(){
        System.out.println("我是"+name+",我的丈夫是"+husband.name);
    }
}

10.this关键字
调用类中的属性
调用类中的方法或构造方法
表示当前对象

public class Notes{
    public static void main(String []args){
        Bear b = new Bear("熊二");
        Bear c = new Bear();
    }
}
class Bear{
    private String name;
    private char sex;
    public Bear(){
        this("熊大",'公');//this直接调用本类中的构造方法                              
    }
    public Bear(String name){
        this(name,'公');
    }
    public Bear(String name,char sex){
        this.name = name;
        this.sex = sex;
        this.bite();//调用本类中的方法,this可以省略
    }
    public void bite(){
        System.out.println("我是" + sex + "熊--" + name);
    }
}

11.static关键字
1.使用static的关键字修饰一个属性,这个变量是一个全局变量
2.在类中定义一个方法为static那么无需本类中的对象即可调用该方法
3.使用static关键字修饰一个类
静态方法和属性在类加载后就存到方法区内存中,此时还没有产生对象,
普通的方法和属性都是属于对象的
声明为static的方法:
仅能调用其他的static方法,可以被普通的方法调用
只能访问static的数据
不能以任何的方式引用this或super(属于对象)

public class Notes{
    public static void main(String []args){
    Mistress m1 = new Mistress("张三");
    Mistress m2 = new Mistress("李四");
    m1.desc();
    m2.desc();
    Mistress.profession = "小三";//使用类名修改静态属性,常用方法
    m1.desc();
    m2.desc();
    Mistress.promosion();
    m1.desc();
    m2.desc();
    }
}

class Mistress{
    String name;
    //String profession = "情人";
    static String profession = "情人";//变成静态属性,不属于对象的属性,属于类; 
    public Mistress(String name){
        this.name = name;
    }
    public void desc(){
        System.out.println("我的名字是"+name+",我的职业是"+profession);
    }
    //使用static关键字修饰一个方法,该方法属于类,不属于对象
    public static void promosion(){
        System.out.println("转正了");
        profession = "主妇";
    }
}

*/
/**
13.蛋疼的数羊
静态变量的使用
*/
/*

public class Notes{
    public static void main(String []args){
        Sheep a = new Sheep();
        Sheep b = new Sheep();
        System.out.println(Sheep.cntSheep());
    }

}
class Sheep{
    private String name;
    private int age;
    static int cnt = 0;
    public Sheep(){
        this("喜羊羊",10);
    }
    public Sheep(String name){
        this(name,10);
    }
    public Sheep(String name, int age){
        this.name = name;
        this.age = age;
        cnt++;
    }
    public static int cntSheep(){
        return cnt;
    }
}

14.对象数组的使用

import java.util.Arrays;
public class Notes{
    public static void main(String []args){
        MonkeyManager.add(new Monkey("悟空"));
        MonkeyManager.add(new Monkey("悟饭"));
        MonkeyManager.add(new Monkey("悟静",'母'));
        MonkeyManager.add(new Monkey("淑敏",'母'));
        MonkeyManager.list();
        MonkeyManager.delete("悟空");
        System.out.println("==============");
        MonkeyManager.list();        
        System.out.println("==============");
        Monkey m = MonkeyManager.find("淑敏");
        m.print();
        System.out.println("==============");
        MonkeyManager.set(new Monkey("悟静",'母'));
        MonkeyManager.list();    
        MonkeyManager.add(new Monkey("小红",'母'));
        MonkeyManager.add(new Monkey("小白",'公'));
        MonkeyManager.add(new Monkey("小黑",'公'));
        System.out.println("==============");
        MonkeyManager.list();    
    }
}
class MonkeyManager{
    private static int cnt = 0;
    private static int n = 5;
    private static Monkey [] monkeys = new Monkey[n];
    public static void add(Monkey monkey){
        if(cnt >= n){
            int new_lenth = monkeys.length*3/2+1;
            monkeys = Arrays.copyOf(monkeys, new_lenth);
        }
        monkeys[cnt] = monkey;
        cnt++;    
    }
    public static void list(){
        for(int i = 0; i < cnt; i++){
            monkeys[i].print();
        }
    }
    public static void delete(String name){
        for(int i = 0; i < cnt; i++){
            if(monkeys[i].getName().equals(name)){
                monkeys[i] = monkeys[cnt-1];
                monkeys[cnt-1] = null;
                cnt--;
            }
        }
    }
    public static Monkey find(String name){
        for(int i = 0; i < cnt; i++){
            if(monkeys[i].getName().equals(name)){
                return monkeys[i];
            }
        }
        return null;
    }
    public static void set(Monkey monkey){
        Monkey m = find(monkey.getName());
        m.setSex(monkey.getSex());
    }
}
class Monkey{
    private String name;
    private char sex;
    //省略get, set方法
    public Monkey(){
        this("齐齐",'公');
    }
    public Monkey(String name){
        this(name,'公');
    }
    public Monkey(String name, char sex){
        this.name = name;
        this.sex = sex;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setSex(char sex){
        this.sex = sex;
    }
    public char getSex(){
        return sex;
    }
    public void print(){
        System.out.println("我是"+sex+"猴--"+name);
    }
}

15.可变参数,(int...num)相当于数组,
如果有一个可变参数和不可变参数,不可变放在首位
静态块执行一次

单例设计模式Test1.java学习记录*/
/**

  1. 类的继承
    属性包括:protected(必须继承的属性),private,public,default

继承使用父类的方法和属性(非私有)
构造方法无法被继承
*/

 public class Notes {
        public static void main(String []args){
            HomeChicken hc = new HomeChicken("小黑");
            hc.desc( );
        }
    }
    class Chicken{
        protected String name;
        protected int age;
        protected Chicken(){
            this("小明",2);
        }
        protected Chicken(String name){
            this(name,10);
        }
        protected Chicken(String name, int age){
            this.name = name;
            this.age = age;
        }
        protected void desc(){
            System.out.println("我是一只鸡,名字叫"+name+",今年"+age+"岁了。");
        }
    }
    class HomeChicken extends Chicken{
        public HomeChicken(){
            super();
        }
        public HomeChicken(String name){
            super(name);
        }
        public HomeChicken(String name, int age){
            super(name, age);
        }
        public void desc(){//方法的重写,方法名,返回值,参数列表相同
            super.desc();//super关键字,相当于this
            System.out.println("我是一只家鸡,名字叫"+name+",今年"+age+"岁了。");
        }
    }

*/
/**
17.final关键字
1.修饰类不能被继承
2.修饰变量为常量
(1).final int n = 3;
(2).在构造方法中赋值
(3).对类中对象赋值是内存地址不变,内容可变
3.修饰方法不能被重写
*/
/**
18.抽象类
(1).多个具有相同特征和行为的类的集合是抽象类
(2).使用abstract声明
(3).不能被实例化
(4).不能使用final修饰
(5).可以没有抽象方法
(6).有抽象方法必须是抽象方法
(7).可以有构造方法
*/
/*

public class Notes{
    public static void main(String [] args){
        Godness g = new Godness();
        g.setName("圆圆");
        g.say();
        UglyWomen u = new UglyWomen();
        u.setName("芳芳");
        u.say();
    }
}

abstract class Women{
    private String name;
    private int age;

    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    
    public void setAge(int age){
        this.age = age;
    }
    public int getAge(){
        return age;
    }
    public abstract void say();
    //public abstract void desc();//必须覆盖抽象类中的所有的抽象方法
}

class Godness extends Women{
    public void say(){
        System.out.println("我是女神,我叫"+getName());
    }
}

class UglyWomen extends Women{
    public void say(){
        System.out.println("我虽丑,但是丑女无敌,我是"+getName());
    }
}

*/
/**
19.接口
1.接口的概念
1)一组行为的规范、定义,没有实现
2)使程序利于变化
3)面向对象中的精髓
4)面向对象的实际法则,基于接口编程
2.接口的定义
interface 声明的类似与类的定义,其中只有常量和抽象方法,
不用abstract修饰
3.接口的规则
1)可以继承多个接口interface A extends B,C,D{}
2)一个类可以实现多个接口,class A implements b,c,d{}
3)命名接口前面加I
4)抽象类实现接口不用实现接口的方法
5)接口中只能使用public,默认为public abstract 可以省略
6)接口中的属性都是常量,默认为public static final都可以省略
常量名通常是全大写
*/
/*

public class Notes{
    public static void main(String [] args){
        Goddess g = new Goddess();
        g.cry();
        g.eat();
        Girl gl = new Girl();
        gl.cry();
        gl.eat();
    }
}
interface IEat{
    public void eat();
}
interface IHit{
    public void cry();
}
class Goddess implements IHit,IEat{
    //实现接口中所有得到方法
    public void cry(){
        System.out.println("好疼呀!!");
    }
    public void eat(){
        System.out.println("一小口一小口的吃");
    }
}
class Girl implements IHit,IEat{
    //实现接口中所有得到方法
    public void cry(){
        System.out.println("卧槽,找死呀!!");
    }
    public void eat(){
        System.out.println("一大口一大口的吃");
    }
}

abstract class Person implements IEat,IHit{
    public void say();
    public void eat();
}

*/
/**
20.多态
1.多态是多种形态
2.两种情况
1)方法的重写和重载
2)对象的多态性
3.实际开发尽量使用父类引用
*/
/*

public class Notes{
    public static void main(String [] name){
        Person man = new Man();//父类的引用指向子类对象,向上转型
        man.say();
        Person women = new Women();
        women.say();
        Man m = (Man)man;//大转小,强制转换;
        m.say();
        //Man mm = (Man)women;
        //java.lang.ClassCastException类型转换失败,不能强转,运行失败
    }
}
abstract class Person{
    private String name;
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public abstract void say();
}        
class Man extends Person{
    public void say(){
        System.out.println("人家是纯爷们");
    }
}    
class Women extends Person{
    public void say(){
        System.out.println("人家是女神");
    }
}

*/
/**
21.instanceof关键字
父类优先考虑接口
尽量不要继承一个具体类
*/
/*

public class Notes{
    public static void main(String [] name){
        Person man = new Man();//父类的引用指向子类对象,向上转型
        //man.say();
        say(man);
        Person women = new Women();
        //women.say();
        say(women);
        Man m = (Man)man;//大转小,强制转换;
        say(m);
        //m.say();
        //Man mm = (Man)women;
        //java.lang.ClassCastException类型转换失败,不能强转,运行失败
    }
    public static void say(Person p){
        p.say();
        //判断p是否是Women对象,是返回真
        if(p instanceof Women){
            Women w = (Women)p;
            w.getAngry();
        }
    }
}
abstract class Person{
    private String name;
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public abstract void say();
}        
class Man extends Person{
    public void say(){
        System.out.println("人家是纯爷们");
    }
}    
class Women extends Person{
    public void say(){
        System.out.println("人家是女神");
    }
    public void getAngry(){
        System.out.println("人家生气了");    
    }
}

*/
/**
22.继承的应用与模版式设计
模版式设计是在父类中构建一个框架,在子类中实现可变的功能
*/
/*

import java.util.Random;
public class Notes{
    public static void main(String []args){
        lingjiuPalace pl = new sheMale();
        pl.action();
    }
}

abstract class lingjiuPalace{
    public void action(){
        if(competition()){//调用自身的方法形成框架
            System.out.println("恭喜你,进入灵鹫宫!");
        }else{
            System.out.println("抱歉,您失败了!");
        }
    }
    public abstract boolean competition();
}
class sheMale extends lingjiuPalace{
    Random r = new Random();
    public boolean competition(){
        return r.nextBoolean();
    }
}

*/
/**
23.接口的应用与策略设计模式
策略设计模式,封装一系列的行为,抽象为接口,可变的行为
OO原则
1.面向接口的编程
2.封装变化
3.多用组合,少用继承
*/
/*

public class Notes{
    public static void main(String[]args){
        Person p = new Person("小白");
        p.setIsay(new BeforeGong());
        p.say();
        p.setIsay(new AfterGong());
        p.say();
    }
}
//这是接口的方式实现
interface Isay{
    public void say();
}
class BeforeGong implements Isay{
    public void say(){
        System.out.println("纯爷们!");
    }
}
class AfterGong implements Isay{
    public void say(){
        System.out.println("宫女!");
    }
}
class Person{
    private String name;
    private Isay isay;//接口相当于一个类型,作为一个属性引入
    public void setIsay(Isay isay){
        this.isay = isay;
    }
    public Person(String name){
        this.name = name;
    }
    public void say(){
        isay.say();            
    }
}


//利用抽象类实现,调用方式省略了,只展示部分代码
abstract class Person{
    private String name;
    public Person(String name){
        this.name = name;
    }
    public abstract void say();
}

class BeforeGongPerson extends Person{
    public BeforeGongPerson(String name){
        super(name);
    }
    public void say(){
        System.out.println("纯爷们!");
    }
}

class AfterGongPerson extends Person{
    public AfterGongPerson(String name){
        super(name);
    }
    public void say(){
        System.out.println("宫女!");
    }
}

/**
24.Object类
1.所有类的父类
2.自动调用toString
3.equals(),自反性(自己和自己比)、对称性(前后可以互换)、
传递性(a=b,b=c,则a=c)、一致性(如果字符布变,那比较结果不变))
4.使用Object类型传输数据
*/
/*
public class Notes{

public static void main(String [] args){
    Baboon bb = new Baboon("小白",7,'公');
    System.out.println(bb);
    Baboon b2 = new Baboon("小白",7,'公');
    System.out.println(bb.equals(b2));
    method(bb);
} 
public static void method(Object obj){
    if(obj instanceof Baboon){
        Baboon bb = (Baboon) obj;
        bb.eat();
    }
}

}

class Baboon{

private String name;
private int age;
private char sex;
public Baboon(String name, int age, char sex){
    this.name = name;
    this.age = age;
    this.sex = sex;
}
public void eat(){
    System.out.println("猴子喜欢吃香蕉!");
}

public String toString(){
    return "我是"+sex+"狒狒"+name+",今年"+age+"岁了。";
}

public boolean equals(Object obj){
    //内存地址相等则为同一对象
    if(this==obj){
        return true;
    }
    
    if(obj instanceof Baboon){
        Baboon ob =(Baboon)obj;
        if(!this.name.equals(ob.name)){
            return false;
        }else if(this.age!=ob.age){
            return false;
        }else if(this.sex!=ob.sex){
            return false;
        }
        return true;
    }else{
        return false;
    }
}

}
*/
/**
25.简单工厂模式
由工厂对象决定创建出哪一种产品类的实例
*/
/*

public class Notes{
    public static void main(String [] args){
        Doll cd = DollFactory.getDoll("cloth");
        if(cd.getInfo()!=null)
            System.out.println(cd.getInfo());
        Doll bd = DollFactory.getDoll("barbie");
        if(bd.getInfo()!=null)
            System.out.println(bd.getInfo());
    }
}

interface Doll{
    public String getInfo();
}

class DollFactory{
    public static Doll getDoll(String name){
        if("cloth".equals(name))
            return new ClothDoll();
        else if("barbie".equals(name))
            return new BarbieDoll();
        else return null;
    }
}

class ClothDoll implements Doll{
    public String getInfo(){
        return "我是一个布娃娃";
    }
}

class BarbieDoll implements Doll{
    public String getInfo(){
        return "我是一个芭比娃娃";
    }
}

*/
/**
26.静态代理模式
在代理中可以设置一些控制方法
*/
/*

import java.util.Scanner;
public class Notes{
    public static void main(String [] args){
        Person p = new Person("小白");
        Matchmaker m = new Matchmaker(p);
        m.miai();
    } 
}

interface Subject{
    public void miai();
}

class Person implements Subject{
    private String name;
    public Person(String name){
        this.name = name;
    }
    public void miai(){
        System.out.println(name+"正在相亲中···");
    }
}

class Matchmaker implements Subject{
    private Subject target;
    
    public Matchmaker(Subject target){
        this.target = target;
    }
    public void before(){
        System.out.println("为代理人匹配如意郎君");
    }
    public void after(){
        System.out.println("本次相亲结束");
        
    }
    public void miai(){
        before();
        Scanner in = new Scanner(System.in);
        System.out.println("你给我多少钱?");
        int key = in.nextInt();
        if(key > 100000)
            target.miai();
        after();
    }
}

*/
/**
27.适配器模式
将一种类型转换为可以利用的类型
*/
/*

public class Notes{
    public  static void main(String [] args){
        PowerA a = new PowerAImpl();
        start(a);
        PowerB b = new PowerBImpl();
        PowerAAdapeter pa = new PowerAAdapeter(b);
        start(pa);
    }
    public static void start(PowerA powera){
        powera.start();
    }
}
class PowerAAdapeter implements PowerA{
    private PowerB powerb;
    public PowerAAdapeter(PowerB pb){
        this.powerb = pb;
    }
    public void start(){
        powerb.connect();
    }
}
interface PowerA{
    public void start();
}

interface PowerB{
    public void connect();
}

class PowerAImpl implements PowerA{
    public void start(){
        System.out.println("电源A已经开始工作···");
    }
}
class PowerBImpl implements PowerB{
    public void connect(){
        System.out.println("电源B已经开始工作···");
    }
}

*/
/**
28.内部类
1.类中内部的类
*/
//1.成员内部类
/*

public class Notes{
    public static void main(String [] args){
        
        Dog dg = new Dog("小白");
        dg.desc();
        dg.childTalk();
        /*外部定义内部类
        Dog.ChildDog child = null;
        child = dg.new ChildDog();
        child.say();
        
    }
}

//内部类(成员内部类)
class Dog{
    private String name;
    public Dog(String name){
        this.name = name;
    }
    public void desc(){
        System.out.println("我是一只狗,主人叫我"+name);
    }
    
    
    
    class ChildDog{
        
        public void say(){
            System.out.println("我是一只狗狗,我妈是"+name);
        }
    }
    public void childTalk(){
        ChildDog cd = new ChildDog();
        cd.say();
    }
}

*/
//2.方法内部类

public class Notes{
    public static void main(String [] args){
        Dog dg = new Dog("小白");
        dg.desc();
        Child cd = dg.childTalk();
        cd.talk();
    }
}

/*
方法内部类
1.只能在定义内部类的方法(childTalk)内实例化
2.方法内部类对象不能使用该方法(childTalk)内的非final局部变量
*//*
interface Child{
    public void talk();
}
class Dog{
    private String name;
    public Dog(String name){
        this.name = name;
    }
    public void desc(){
        System.out.println("我是一只狗,主人叫我"+name);
    }
    Child child;
    //在方法里面声明一个内部类,其中使用的变量全部是final
    public Child childTalk(){
            class ChildDog implements Child {
                public void talk(){
                    System.out.println("我是一只狗狗,我妈是"+name);
            }
        }
        ChildDog c = new ChildDog();
        c.talk();
        return c;
    }
}*/

//3.静态内部类,一个静态内部类相当于一个外部类

public class Notes{
    public static void main(String [] args){
        Dog.ChildDog child = new Dog.ChildDog();
        child.talk();
    }
}

class Dog{
    private String name;
    public Dog(){}
    public Dog(String name){
        this.name = name;
    }
    public void desc(){
        System.out.println("我是一只狗,主人叫我"+name);
    }
    //privat,static 只能使用在内部类
    static class ChildDog{
        public void talk(){
            System.out.println("我是一只狗狗");
        }
    }    
}

//4.匿名内部类
/*原则
1.不能有构造方法,只能有一个实例
2.不能定义静态成员,方法
3.不能是public,protected,static,private
4.一定在new后
5.局部的使用
*/
/*

public class Notes{
    public static void main(String [] args){
        //(1)继承式内部类
        Dog dog = new Dog("小白"){
            public void desc(){
                System.out.println("我是一只母狗,主人叫我"+getName());
            }
        };
        dog.desc();
        //(2)接口式内部类
        Child child = new Child(){
            public void desc(){
                System.out.println("我是一只狗狗");
            }
        };
        child.desc();
        //(3)参数式的匿名内部类
        dog.childTalk(new Child(){
            public void desc(){
                System.out.println("我是一只小狗狗");
            }        
        });
    }
    /*public static void childTalk(Child c){
        c.desc();
    }*//*
}
interface Child{
    public void desc();
}
class Dog{
    private String name;
    public Dog(){}
    public Dog(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void desc(){
        System.out.println("我是一只狗,主人叫我"+name);
    }
    public void childTalk(Child c){
        c.desc();
    }
}*/

/**
29.链表
*/

//简单的递归实现
public class Notes{
    public static void main(String [] args){
        System.out.println(fact(5));
    }
    public static int fact(int num){
        if(num == 1)
            return 1;
        else{
            return fact(num-1)*num;
        }
    }
}


public class Notes{
    public static void main(String [] args){
        NodeManager nm = new NodeManager();
        nm.addNode("节点1");
        nm.addNode("节点2");
        nm.addNode("节点3");
        nm.addNode("节点4");
        nm.addNode("节点5");
        nm.addNode("节点6");
        nm.addNode("节点7");
        nm.printNode();
        nm.deleteNode("节点3");
        nm.printNode();
    }
}
//链表管理
class NodeManager{
    private Node root;
    public void addNode(String name){
        if(root==null){
            root = new Node(name);
        }else{
            root.add(name);
        }
    }
    public void deleteNode(String name){
        if(root!=null){
            if(root.name.equals(name)){
                root = root.next;
            }
            else{
                root.del(name);
            }
        }
        
    }
    public void printNode(){
        if(root!=null){
            System.out.print(root.name);
            root.print();
            System.out.println();
        }
    }
    class Node{
        private String name;
        private Node next;
        public Node(String name){
            this.name = name;
        } 
        public void add(String name){
            if(this.next==null){
                this.next = new Node(name);
            }else{
                this.next.add(name);
            }
        }
        public void del(String name){
            if(this.next!=null){
                if(this.next.name.equals(name)){
                    this.next = this.next.next;
                }
                else{
                    this.next.del(name);
                }
            }
        }
        public void print(){
            if(this.next!=null){
                System.out.print("-->"+this.next.name);
                this.next.print();
            }
        }
    }
}

zorpan
41 声望14 粉丝

记笔记