Java笔记

No_16_0302 Java基础学习第十天

2016-03-04  本文已影响45人  lutianfei
文档版本 开发工具 测试平台 工程名字 日期 作者 备注
V1.0 2016.03.02 lutianfei none

[TOC]




形式参数和返回值问题

class Student {
    public void study() {
        System.out.println("Good Good Study,Day Day Up");
    }
}

class StudentDemo {
    public void method(Student s) { //ss; ss = new Student();  Student s = new Student();
        s.study();
    }
}

class StudentTest {
    public static void main(String[] args) {
        //需求:我要测试Student类的study()方法
        Student s = new Student();
        s.study();
        System.out.println("----------------");
        
        //需求2:我要测试StudentDemo类中的method()方法
        StudentDemo sd = new StudentDemo();
        Student ss = new Student();
        sd.method(ss);
        System.out.println("----------------");
        
        //匿名对象用法
        new StudentDemo().method(new Student());
    }
}
abstract class Person {
    public abstract void study();
}

class PersonDemo {
    public void method(Person p) {//p; p = new Student();  Person p = new Student(); //多态
        p.study();
    }
}

//定义一个具体的学生类
class Student extends Person {
    public void study() {
        System.out.println("Good Good Study,Day Day Up");
    }
}

class PersonTest {
    public static void main(String[] args) {
        //目前是没有办法的使用的
        //因为抽象类没有对应的具体类
        //那么,我们就应该先定义一个具体类
        //需求:我要使用PersonDemo类中的method()方法
        PersonDemo pd = new PersonDemo();
        Person p = new Student();
        pd.method(p);
    }
}
//定义一个爱好的接口
interface Love {
    public abstract void love();
}

class LoveDemo {
    public void method(Love l) { //l; l = new Teacher();  Love l = new Teacher(); 多态
        l.love();
    }
}

//定义具体类实现接口
class Teacher implements Love {
    public void love() {
        System.out.println("老师爱学生,爱Java,爱林青霞");
    }
}

class TeacherTest {
    public static void main(String[] args) {
        //需求:我要测试LoveDemo类中的love()方法
        LoveDemo ld = new LoveDemo();
        Love l = new Teacher();
        ld.method(l);
    }
}
class Student {
    public void study() {
        System.out.println("Good Good Study,Day Day Up");
    }
}

class StudentDemo {
    public Student getStudent() {
        return new Student();
    }
}

class StudentTest2 {
    public static void main(String[] args) {
        //需求:我要使用Student类中的study()方法
        //但是,这一次我的要求是,不要直接创建Student的对象
        //让你使用StudentDemo帮你创建对象
        StudentDemo sd = new StudentDemo();
        Student s = sd.getStudent(); //new Student(); Student s = new Student();
        s.study();
    }
}
abstract class Person {
    public abstract void study();
}

class PersonDemo {
    public Person getPerson() {
        //Person p = new Student();
        //return p;
        
        return new Student();
    }
}

class Student extends Person {
    public void study() {
        System.out.println("Good Good Study,Day Day Up");
    }
}

class PersonTest2 {
    public static void main(String[] args) {
        //需求:我要测试Person类中的study()方法
        PersonDemo pd = new PersonDemo();
        Person p = pd.getPerson(); //new Student();  Person p = new Student(); 多态
        p.study();
    }
}
interface Love {
    public abstract void love();
}

class LoveDemo {
    public Love getLove() {
        //Love l = new Teacher();
        //return l;
        
        return new Teacher();
    }
}

//定义具体类实现接口
class Teacher implements Love {
    public void love() {
        System.out.println("老师爱学生,爱Java,爱林青霞");
    }
}

class TeacherTest2 {
    public static void main(String[] args) {
        //如何测试呢?
        LoveDemo ld = new LoveDemo();
        Love l = ld.getLove(); //new Teacher(); Love l = new Teacher(); 多态
        l.love();
    }
}

链式编程

/*
    链式编程。
        每次调用完毕方法后,返回的是一个对象。
*/
class Student {
    public void study() {
        System.out.println("Good Good Study,Day Day Up");
    }
}

class StudentDemo {
    public Student getStudent() {
        return new Student();
    }
}

class StudentTest3 {
    public static void main(String[] args) {
        //如何调用的呢?
        StudentDemo sd = new StudentDemo();
        //Student s = sd.getStudent();
        //s.study();
        
        //
        sd.getStudent().study();
    }
}

包的概述(其实就是文件夹)

包的定义及注意事项

导包

修饰符

权限修饰符

类及其组成可以用的修饰符

内部类

内部类概述

内部类的位置

成员内部类
class Outer {
    private int num = 10;
    
    class Inner {
        public void show() {
            System.out.println(num);
        }
    }
}

class InnerClassDemo3 {
    public static void main(String[] args) {
        //格式:外部类名.内部类名 对象名 = 外部类对象.内部类对象;
        Outer.Inner oi = new Outer().new Inner();
        oi.show();
    }
}
class Body {
   private class Heart {
       public void operator() {
           System.out.println("心脏搭桥");
       }
   }
   
   public void method() {
       if(如果你是外科医生) {
           Heart h = new Heart();
           h.operator();
       }
   }
}
成员内部的常见修饰符
class Outer {
    private int num = 10;
    private static int num2 = 100;
    
    //内部类用静态修饰是因为内部类可以看出是外部类的成员
    public static class Inner {
        public void show() {
            //System.out.println(num);
            System.out.println(num2);
        }

        public static void show2() {
            //System.out.println(num);
            System.out.println(num2);
        }        
    }
}

class InnerClassDemo4 {
    public static void main(String[] args) {
        //成员内部类被静态修饰后的访问方式是:
        //格式:外部类名.内部类名 对象名 = new 外部类名.内部类名();
        Outer.Inner oi = new Outer.Inner();
        oi.show();
        oi.show2();
        
        //show2()的另一种调用方式
        Outer.Inner.show2();
    }
}
class Outer {
    public int  num = 10;
    class Inner {
        public int  num = 20;
        public void show() {
            int  num = 30;
            System.out.println(?); //num
            System.out.println(??);//this.num
            System.out.println(???);
            //new Outer().num
            // Outer.this.num
        }
    }
}
局部内部类
class Outer {
    private int num  = 10;
    
    public void method() {
        //int num2 = 20;
        //final int num2 = 20;
        class Inner {
            public void show() {
                System.out.println(num);
                //从内部类中访问本地变量num2; 需要被声明为最终类型
                System.out.println(num2);//20
            }
        }
        
        //System.out.println(num2);
        
        Inner i = new Inner();
        i.show();
    }
}

class InnerClassDemo5 {
    public static void main(String[] args) {
        Outer o = new Outer();
        o.method();
    }
}
匿名内部类
interface Inter {
    public abstract void show();
    public abstract void show2();
}

class Outer {
    public void method() {
        //一个方法的时候
        /*
        new Inter() {
            public void show() {
                System.out.println("show");
            }
        }.show();
        */
        
        //二个方法的时候
        /*
        new Inter() {
            public void show() {
                System.out.println("show");
            }
            
            public void show2() {
                System.out.println("show2");
            }
        }.show();
        
        new Inter() {
            public void show() {
                System.out.println("show");
            }
            
            public void show2() {
                System.out.println("show2");
            }
        }.show2();
        */
        
        //如果我是很多个方法,就很麻烦了
        //那么,我们有没有改进的方案呢?
        Inter i = new Inter() { //多态
            public void show() {
                System.out.println("show");
            }
            
            public void show2() {
                System.out.println("show2");
            }
        };
        
        i.show();
        i.show2();
    }
}

class InnerClassDemo6 {
    public static void main(String[] args) {
        Outer o = new Outer();
        o.method();
    }
}
interface Person {
    public abstract void study();
}

class PersonDemo {
    //接口名作为形式参数
    //其实这里需要的不是接口,而是该接口的实现类的对象
    public void method(Person p) {
        p.study();
    }
}

//实现类
class Student implements Person {
    public void study() {
        System.out.println("好好学习,天天向上");
    }
}

class InnerClassTest2 {
    public static void main(String[] args) {
        //测试
        PersonDemo pd = new PersonDemo();
        Person p = new Student();
        pd.method(p);
        System.out.println("--------------------");
        
        //匿名内部类在开发中的使用
        //匿名内部类的本质是继承类或者实现了接口的子类匿名对象
        pd.method(new Person(){
            public void study() {
                System.out.println("好好学习,天天向上");
            }
        });
    }
}
  interface Inter { 
      void show();
  }
  class Outer { //补齐代码 
  
  }
  
  class OuterDemo {
      public static void main(String[] args) {
        Outer.method().show();
    }
  }
//分析:
/*
1:由Outer.method()可以看出method()应该是outer中的一个静态方法。
2:Outer.method().show()可以看出method()方法的返回值是一个对象
3:由于接口Inter中有一个show()方法,所以认为method()方法的返回值类型是一个接口。
*/

//* 补充代码如下:在不起代码后:
public static Inter method(){
    return new Inter(){
        public void show(){
            System.out.println("HelloWorld");
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读