java学习笔记day10 引用类型作为参数和返回值、包与导包、修饰符 、内部类

java学习笔记day10

思维导图

引用类型作为参数和返回值 、包与导包 、修饰符 、内部类

一、运动员与教练案例

在这里插入图片描述
源代码

/*
	教练和运动员案例(学生分析然后讲解)
		乒乓球运动员和篮球运动员。
		乒乓球教练和篮球教练。
		为了出国交流,跟乒乓球相关的人员都需要学习英语。
		请用所学知识:
		分析,这个案例中有哪些抽象类,哪些接口,哪些具体类。
	
	整个分析过程,我是通过画图讲解的。	
*/
//定义一个说英语的接口
interface SpeakEnglish {
	//说英语
	public abstract void speak();
}

//定义人的抽象类
abstract class Person {
	private String name;
	private int age;
	
	public Person() {}
	
	public Person(String name,int age) {
		this.name = name;
		this.age = age;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public int getAge() {
		return age;
	}
	
	public void setAge(int age) {
		this.age = age;
	}
	
	//睡觉
	public void sleep() {
		System.out.println("人都是要睡觉的");
	}
	
	//吃饭
	public abstract void eat();
}

//定义运动员抽象类
abstract class Player extends Person {
	public Player() {}
	
	public Player(String name,int age) {
		super(name,age);
	}
	
	//学习
	public abstract void study();
}

//定义教练抽象类
abstract class Coach extends Person {
	public Coach() {}
	
	public Coach(String name,int age) {
		super(name,age);
	}
	
	//教
	public abstract void teach();
}

//定义乒乓球运动员具体类
class PingPangPlayer extends Player implements SpeakEnglish {
	public PingPangPlayer(){}
	
	public PingPangPlayer(String name,int age) {
		super(name,age);
	}
	
	//吃
	public void eat() {
		System.out.println("乒乓球运动员吃大白菜,喝小米粥");
	}
	
	//学习
	public void study() {
		System.out.println("乒乓球运动员学习如何发球和接球");
	}
	
	//说英语
	public void speak() {
		System.out.println("乒乓球运动员说英语");
	}
}

//定义篮球运动员具体类
class BasketballPlayer extends Player {
	public BasketballPlayer(){}
	
	public BasketballPlayer(String name,int age) {
		super(name,age);
	}
	
	//吃
	public void eat() {
		System.out.println("篮球运动员吃牛肉,喝牛奶");
	}
	
	//学习
	public void study() {
		System.out.println("篮球运动员学习如何运球和投篮");
	}
}

//定义乒乓球教练具体类
class PingPangCoach extends Coach implements SpeakEnglish {
	public PingPangCoach(){}
	
	public PingPangCoach(String name,int age) {
		super(name,age);
	}
	
	//吃
	public void eat() {
		System.out.println("乒乓球教练吃小白菜,喝大米粥");
	}
	
	//教
	public void teach() {
		System.out.println("乒乓球教练教如何发球和接球");
	}
	
	//说英语
	public void speak() {
		System.out.println("乒乓球教练说英语");
	}
}

//定义篮球教练具体类
class BasketballCoach extends Coach {
	public BasketballCoach(){}
	
	public BasketballCoach(String name,int age) {
		super(name,age);
	}
	
	//吃
	public void eat() {
		System.out.println("篮球教练吃羊肉,喝羊奶");
	}
	
	//教
	public void teach() {
		System.out.println("篮球教练教如何运球和投篮");
	}
}

class InterfaceDemo {
	public static void main(String[] args) {
		//测试运动员(乒乓球运动员和篮球运动员)
		//乒乓球运动员
		PingPangPlayer ppp = new PingPangPlayer();
		ppp.setName("王浩");
		ppp.setAge(33);
		System.out.println(ppp.getName()+"---"+ppp.getAge());
		ppp.eat();
		ppp.sleep();
		ppp.study();
		ppp.speak();
		System.out.println("----------------");
		//通过带参构造给数据(留给你们)
		
		//篮球运动员
		BasketballPlayer bp = new BasketballPlayer();
		bp.setName("姚明");
		bp.setAge(34);
		System.out.println(bp.getName()+"---"+bp.getAge());
		bp.eat();
		bp.sleep();
		bp.study();
		//bp.speak(); //没有该方法
		
		//测试教练自己做
	}
}

二、引用类型作为形式参数和返回值⭐

1.形式参数是引用类型
在这里插入图片描述
类名形参

//1.类名形参
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());
	}
}

抽象类形参

//2.抽象类形参
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);
	}
}

接口形参

//3.接口形参
//定义一个爱好的接口
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);
	}
}

2.返回值是引用类型
在这里插入图片描述

类名返回值

//1.类名返回值
class Student {
	public void study() {
		System.out.println("Good Good Study,Day Day Up");
	}
}

class StudentDemo {
	public Student getStudent() {
		//Student s = new Student();
		//Student ss = s;
		
		//Student s = new Student();
		//return s;
		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();
	}
}

抽象类返回值

//2.抽象类返回值
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();
	}
}

接口返回值

//3.接口返回值
//定义一个爱好的接口
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();
	}
}

三、包与导包

在这里插入图片描述
在这里插入图片描述
导包
在这里插入图片描述

四、修饰符⭐【P279】

在这里插入图片描述
在这里插入图片描述

五、内部类

在这里插入图片描述
在这里插入图片描述
1.成员内部类【P282】
在这里插入图片描述
成员内部类的修饰符及其应用

/*
	成员内部类的修饰符:
		private 为了保证数据的安全性
		static 为了方便访问数据
			注意:静态内部类访问的外部类数据必须用静态修饰。
	
	案例:我有一个人(人有身体,身体内有心脏。)
		
		class Body {
			private class Heart {
				public void operator() {
					System.out.println("心脏搭桥");
				}
			}
			
			public void method() {
				if(如果你是外科医生) {
					Heart h = new Heart();
					h.operator();
				}
			}
		}
		
		按照我们刚才的讲解,来使用一下
		Body.Heart bh = new Body().new Heart();
		bh.operator();
		//加了private后,就不能被访问了,那么,怎么玩呢?
		Body b =  new Body();
		b.method();
*/
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) {
		//使用内部类
		// 限定的新静态类
		//Outer.Inner oi = new Outer().new Inner();
		//oi.show();
		//oi.show2();
		
		//成员内部类被静态修饰后的访问方式是:
		//格式:外部类名.内部类名 对象名 = new 外部类名.内部类名();
		Outer.Inner oi = new Outer.Inner();
		oi.show();
		oi.show2();
		
		//show2()的另一种调用方式
		Outer.Inner.show2();
	}
}

成员内部类的面试题

/*
	面试题:
		要求请填空分别输出30,20,10。
		
	注意:
		1:内部类和外部类没有继承关系。
		2:通过外部类名限定this对象
			Outer.this
*/
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);
			System.out.println(Outer.this.num);
		}
	}
}
class InnerClassTest {
	public static void main(String[] args) {
		Outer.Inner oi = new Outer().new Inner();
		oi.show();
	}	
}

2.局部内部类【P285】
在这里插入图片描述
局部内部类面试题

/*	
	
	面试题:
		局部内部类访问局部变量的注意事项?
		A:局部内部类访问局部变量必须用final修饰
		B:为什么呢?
			局部变量是随着方法的调用而调用,随着调用完毕而消失。
			而堆内存的内容并不会立即消失。所以,我们加final修饰。
			加入final修饰后,这个变量就成了常量。既然是常量。你消失了。
			我在内存中存储的是数据20,所以,我还是有数据在使用。
*/
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();
	}
}

3.匿名内部类
在这里插入图片描述
匿名内部类的方法调用 【P287】

//实例代码
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("好好学习,天天向上");
			}
		});
	}
}

匿名内部类面试题 (难得一B)【P289】

/*
	匿名内部类面试题:
		按照要求,补齐代码
			interface Inter { void show(); }
			class Outer { //补齐代码 }
			class OuterDemo {
				public static void main(String[] args) {
					  Outer.method().show();
				  }
			}
			要求在控制台输出”HelloWorld”
*/
interface Inter { 
	void show(); 
	//public abstract
}

class Outer { 
	//补齐代码
	public static Inter method() {
		//子类对象 -- 子类匿名对象
		return new Inter() {
			public void show() {
				System.out.println("HelloWorld");
			}
		};
	}
}

class OuterDemo {
	public static void main(String[] args) {
		Outer.method().show();
		/*
			1:Outer.method()可以看出method()应该是Outer中的一个静态方法。
			2:Outer.method().show()可以看出method()方法的返回值是一个对象。
				又由于接口Inter中有一个show()方法,所以我认为method()方法的返回值类型是一个接口。
		*/
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值