Day09Java基础学习

Day09Java基础学习

  1. 面向对象(多态的概述及其代码体现)
    (1) 多态的概述:事物存在多种形态。
    (2) 多态前提:
    要有继承关系;要有方法重写;要有父类引用指向子类对象。
    (3) 案例演示:代码体现多态

class  Demo_Polymorphic{
	public static void main(String[] args) {
		Cat c = new Cat();
		c.eat();
		Animal a = new Cat(); //父类引用指向子类对象
	    a.eat();   //输出猫吃鱼
	}
}
class Animal{
	public void eat() {
	System.out.println("动物吃饭");
	}
}
class Cat extends Animal {
	public void eat() {
		System.out.println("猫吃鱼");
	}
}
  1. 面向对象(多态中的成员访问特点)
    (1) 多态中的成员访问特点:
    成员变量:编译看左边(父类),运行看右边(子类)。
    成员方法:编译看左边(父类),运行看右边(子类)。
    静态方法:编译看左边(父类),运行看左边(父类)。
    (静态和类相关,算不上重写,所以,访问还是左边的)
    *只有非静态的成员方法,编译看左边,运行看右边。
    (2)案例演示:多态中的成员访问特点。
class  Demo_Polymorphic{
	public static void main(String[] args) {
	 Father f = new Son();
	 System.out.println(f.num); //输出10
	Son s = new Son();
	System.out.println(s.num);//输出20
	}
}
class Father{
	int num = 10;
}

class Son extends Father {
	int num = 20;
}
  1. 面向对象(超人的故事)
    (1)案例分析
class  Demo3_SuperMan{
	public static void main(String[] args) {
	 Person p = new SuperMan();
	 System.out.println(p.name);//输出Jone
	 p.谈生意();  //输出谈几个亿的大单子
	 p.fly(); // 编译不通过
}

class Person {
	String name = "Jone";
	public void 谈生意() {
		System.out.println("谈生意");
	}
}
class SuperMan extends Person {
String name = "superMan";
	public void 谈生意() {
		System.out.println("谈几个亿的大单子");
	}
	public void fly() {
		System.out.println("飞出去救人");
	}
}
  1. 面向对象(多态中向上转型和向下转型)
    (1) 案例
Person p = new SuperMan();向上转型
SuperMan sm = (SuperMan)p;向下转型
class  Demo3_SuperMan{
	public static void main(String[] args) {
	 Person p = new SuperMan(); //父类引用指向子类对象,向上转型

	 System.out.println(p.name);//输出Jone
	 p.谈生意();  //输出谈几个亿的大单子
	 SuperMan sm = (SuperMan)p; //向下转型
     sm.fly(); //输出飞出去救人
}

class Person {
	String name = "Jone";
	public void 谈生意() {
		System.out.println("谈生意");
	}
}

class SuperMan extends Person {
	String name = "superMan";
	public void 谈生意() {
		System.out.println("谈几个亿的大单子");
	}
	public void fly() {
		System.out.println("飞出去救人");
	}
}
  1. 面向对象(多态的好处和弊端)
    (1) 多态的好处
    a. 提高代码的维护性(继承保证)
    b. 提高代码的扩展性(由多态保证)
    (2) 案例演示
    多态的好处:可以当作形式参数,可以接收任意子类对象
    多态的弊端:不能时延子类的特有属性和行为。
class  Demo4_Animal {
	public static void main(String[] args) {
	 method(new Cat());
	 method(new Dog());
	}
	/*public static void method(Cat c) {
		c.eat();
	}
	public static void method(Dog d) {
		d.eat();
	}*/
	public static void method(Animal a) {
		a.eat();
	}
}

class Animal {
	public void eat() {
		System.out.println("动物吃饭");
	}
}

class Cat extends Animal {
	public void eat() {
		System.out.println("猫吃鱼");
	}
	public void catchMouse() {
		System.out.println("抓老鼠");
	}
}

class Dog extends Animal {
	public void eat() {
		System.out.println("狗吃肉");
	}
	public void lookHome() {
		System.out.println("狗看家");
	}
}

(3) 案例演示

Method(Animal a)
Method(Cat c)
例子:
class  Demo4_Animal {
	public static void main(String[] args) {
	 method(new Cat());
	 method(new Dog());
	}
	/*public static void method(Cat c) {
		c.eat();
	}
	public static void method(Dog d) {
		d.eat();
	}*/
	public static void method(Animal a) {
		//a.eat();
		//Cat c = (Cat) a;//如果把狗强转成猫,就会出现强转异常。
		//关键字 instanseof 判断前边的引用是否是后边的数据类型
		if (a instanseof Cat) {
			Cat c = (Cat)a;
			a.eat();
			a.catchMouse();
		}else if (a instanseof Dog) {
			Dog d = (Dog)a;
			d.eat();
			d.lookHome();
		}else {
			a.eat();
		}	
	}
}

class Animal {
	public void eat() {
		System.out.println("动物吃饭");
	}
}

class Cat extends Animal {
	public void eat() {
		System.out.println("猫吃鱼");
	}
	public void catchMouse() {
		System.out.println("抓老鼠");
	}
}

class Dog extends Animal {
	public void eat() {
		System.out.println("狗吃肉");
	}
	public void lookHome() {
		System.out.println("狗看家");
	}
}
  1. 面向对象(多态中的题目分析题)
    (1)例子1
class Test1Demo {
	public static void main(String args) {
		Fu f = new Zi();
		f.method();  //编译会报错,编译看父类
		f.show(); //编译通过,运行看子类,输出zi show
	}
}

class Fu {
		public void show() {
		 	System.out.println("fu show");
		}
}
class Zi extends Fu {
		public void show() {
			System.out.println("zi show");
	}

	public void method() {
		System.out.println("zi method");
	}
}

(2) 例子2

 class Test2DuoTai {
	public static void main(String [] args) {
		A a = new B();
		a.show();  //编译可以通过,运行时看子类输出“爱”

		B b = new C();
		b.show();//编译走父类,运行走子类输出了“你”
	}
}
class A {
	public void show() {
		show2();
	}
	public void show2() {
		System.out.println("我");
	}
}

class B extends A {
	public void show() {
		show2();
	}
	public void show2() {
		System.out.println("爱");
	}
}
class C  extends B {
	public void show() {
		super.show();
	}
	public void show2() {
		System.out.println("你");
	}
}
  1. 面向对象(抽象类的概述及特点)
    (1) 抽象类的概述:抽象的就是看不懂的。
    (2) 抽象类的特点:
    a. 抽象类和抽象方法必须用abstract关键字修饰
    abstract class 类名 {}
    public static void eat();
    b. 抽象类不一定有抽象方法,有抽象类方法的类一定使抽象类或者是接口。
    c. 抽象类不能实例化,那么抽象类如何实例化呢?
    按照多态的方式,由具体的子类实例化。其实这也是多态的一种,抽象类多态。
    d. 抽象类的子类
    要么是抽象类
    要么重写抽象类中的所有抽象方法
    (3) 案例演示:抽象类的特点
class  Demo1_Abstract {
	public static void main(String[] args) {
		Animal a = new Cat();
		a.eat(); //抽象类可以通过子类调用
	}
}

abstract class Animal {				//抽象类
	public abstract void eat();		//抽象方法 
}

class Cat extends Animal {
	public void eat() {
		System.out.println("猫吃鱼");
}
  1. 面向对象(抽象类的成员特点)
    (1) 抽象类的成员特点
    a. 成员变量:既可以是变量,也可以是常量。abstract是否可以修饰成员变量?不能修饰成员变量
    b. 构造方法:有。
    用于子类访问父类数据的初始化。
    c. 成员方法:既可以是抽象的,也可以是非抽象的。
    (2) 案例演示:抽象类的成员特点。
    (3) 抽象类的成员方法特性:
    抽象方法 强制要求子类做的事情。
    非抽象方法 子类继承的事情,提高代码的复用性。
class  Demo2_Abstract {
	public static void main(String[] args) {
		Animal a = new Cat();
		a.eat(); //抽象类可以通过子类调用
	}
}

abstract class Demo {				//抽象类
	int num = 10;
	final int num = 20;

	public Demo() {}
	
	public void print() {
		System.out.println("111");
	}

	public abstract void method(); 
}

class Test extends Demo{
	public void method() {
		System.out.println("111");
	}
}
class Cat extends Animal {
	public void eat() {
		System.out.println("猫吃鱼");
}
  1. 面向对象(葵花宝典)
    案例演示:抽象类的作用。
class  Demo3_葵花宝典 {
	public static void main(String[] args) {
		岳不群 小岳子 = new 岳不群();
		小岳子.自宫(); //输出“用牙签”
	}
}
 abstract class 葵花宝典 {
	 public abstract void 自宫();
 }
class 岳不群 extends 葵花宝典 {
	public void 自宫() {
		System.out.println("用牙签");
	}
}
class 林平之 extends 葵花宝典 {
	public void 自宫() {
		System.out.println("用指甲刀");
	}
}
class 东方不败 extends 葵花宝典 {
	public void 自宫() {
		System.out.println("用锤子,不忍直视");
	}
}
  1. 面向对象(抽象类的练习)
    案例:
    具体事物:猫,狗
    共性:姓名,年龄,吃饭
    猫的特性:抓老鼠、
    狗的特性:看家
class  Test1_Animal {
	public static void main(String[] args) {
	Cat c = new Cat("加菲",8);
	System.out.println(c.getName() + "..." + c.getAge());
	c.eat();
	c.catchMouse();


   Dog d = new Cat("八公",30);
	System.out.println(d.getName() + "..." + d.getAge());
	d.eat();
	d.lookHome();
	}
}
 abstract class Animal {
	private String name;  //姓名
	private int age;	//年龄

	public	Animal() {}  //空参构造

	public Animal (String name ,int age) { //有参
		this.name = name;
		this.age = age;
	}
	 public void setName(String name) { //设置姓名
		 this.name = name;
	 }
	 public void getName() {  //获取姓名
		 return name;
	 }
	 public void setAge(int age ) { //设置年龄
		 this.age = age;
	 }
	 public void getAge() { //获取姓名
		 return age;
	 }
     public abstract void eat(); //吃饭方法抽象

}

class Cat extends Animal {
	public	Cat() {}  //空参构造

	public Cat (String name ,int age) { //有参
		super(name,age);
	}
	public void eat() {
		System.out.println("猫吃鱼");
	}
	public void catchMouse() {
		System.out.println("抓老鼠");
	}
}

class Dog extends Animal {
	public	Dog() {}  //空参构造

	public Dog (String name ,int age) { //有参
		super(name,age);
	}
	public void eat() {
		System.out.println("狗吃肉");
	}
	public void lookHome() {
		System.out.println("狗看家");
	}
}
  1. 面向对象(抽象类的练习老师案例)
    案例:
    具体事物:基础班老师,就业班老师
    共性:姓名,年龄,讲课
class  Test1_Teacher {
	public static void main(String[] args) {
	BaseTeacher bt = new BaseTeacher("冯佳",18);
	bt.teach();
	}
}
 abstract class Teacher {
	private String name;  //姓名
	private int age;	//年龄
	public	Teacher() {}  //空参构造
	public Teacher (String name ,int age) { //有参
		this.name = name;
		this.age = age;
	}
	 public void setName(String name) { //设置姓名
		 this.name = name;
	 }
	 public void getName() {  //获取姓名
		 return name;
	 }
	 public void setAge(int age ) { //设置年龄
		 this.age = age;
	 }
	 public void getAge() { //获取年龄
		 return age;
	 }
     public abstract void teach(); //讲课方法抽象
}
class BaseTeacher extends Teacher{
	public	BaseTeacher() {}  //空参构造
	public BaseTeacher (String name ,int age) { //有参
		super(name,age);
	}
	public void teach() {
		System.out.println("我的姓名是:" + this.getName() + ",我的年龄是:" + this.getAge() + ",讲的内容是java基础");
	}
}
class WorkTeacher extends Teacher {
	public WorkTeacher() {}
	public WorkTeacher(String name,int age) {
		super(name,age);
	}
	public void teach() {
		System.out.println("我的姓名是:" + this.getName() + ",我的年龄是:" + this.getAge() + ",讲的内容是java就业");
	}
}
具体事物:基础班学生,就业班学生
共性:姓名,年龄,学习
class  Test2_Student {
	public static void main(String[] args) {
	BaseStudent st = new BaseStudent("lj",11);
	st.study();
	WorkStudent st1 = new WorkStudent("jiu",22);
	st1.Study();
	}
}
 abstract class Teacher {
	private String name;  //姓名
	private int age;	//年龄

abstract class Student {
	private String name; //姓名
	private int age; //年龄

   public Student() {}  //空参构造

   public Student (String name,int age) { // 有参构造
	   this.name = name;
	   this.age = age;
   }
   public void setName(String name) { //设置姓名
	   this.name = name;
   }
   public void getName() { //获取姓名
	   return name;
   }
   public void setAge(int age) { //设置年龄
	   this.age = age;
   }
   public void getAge() { //获取年龄
	   return age;
   }
   public abstract study();
}
class BaseStudent extends Student {
	public BaseStudent() {}

	public BaseStudent(String name,int age) {
		super(name,age);
	}
	public void study() {
		System.out.println("我的名字是"  + this.getName() + "我的年龄是" + this.getAge() + "学的是java基础");
	}
}
class WorkStudent extends Student {
	public WorkStudent() {}

	public WorkStudent (String name,int age) {
		super(name,age);
	}
	public void study() {
		System.out.println("我的名字是" + this.getName() + "我的年龄是" + this.getAge() + "学的是java就业");
	}
}
  1. 面向对象(抽象类练习员工案例)
    (1) 案例演示
    假如我们在开发一个系统时需要对程序员类型进行设计,程序包含3个属性:姓名、工号及工资。
    经理,除了含有程序员的属性外,另外还有一个奖金属性。
    请使用继承的思想设计程序员类和经理类。要求类中提供必要的方法进行属性访问。
class  Test3_Employee{
	public static void main(String[] args) {
		Corder c = new Corder("德玛西亚","007",8000);
		c.work();

		Manger m = new Manager("苍老师","9527",3000,20000)
		m.work();
	}
}
abstract class Employee {
	private String name;
	private String id;
	private double salary;

	public Employee() {}

	public Employee(String name,String id,double salary) {
		this.name = name;
		this.id = id;
		this.salary = salary;
	}

	public void setName(String name) {
		this.name = name;
	}
	public void getName() {
		return name;
	}
	public void setId(String id) {
		this.id = id;
	}
	public void getId() {
		return id;
	}
	public void setsalary(double salary) {
		this.salary = salary;
	} 
	public void getSalary() {
		return salary;
	}
	public abstract void work();

}
class Coder extends Employee {
	public Coder() {}

	public Coder(String name,String id,double salary) {
		super(name,id,salary);
	}
	public void work() {
		System.out.println("我的姓名是" + this.getName() + "我的工号是" + this.getId() + "我的工资是" + this.getSalary + "我的工作内容是敲代码");
	}
}
 class Manger extends Employee {
	 private int bonus;
	public Coder() {}

	public Coder(String name,String id,double salary) {
		super(name,id,salary);
		this.bonus = bonus;
	}
	public void work() {
		System.out.println("我的姓名是" + this.getName() + "我的工号是" + this.getId() + "我的工资是" + this.getSalary + "我的奖金是" + bonus + "我的工作内容是管理");
	}
}
  1. 面向对象(抽象类中的面试题)
    (1) 面试题1
    一个抽象类如果没有抽象方法,可不可以定义为抽象类?如果可以,有什么意义?
    可以,这么做的目的只有一个,就是不让其他类创建本类对象,交给子类完成。
    (2) 面试题2
    abstract不能和哪些关键字共存。
    a) abstract 和static:被abstract修饰的方法没有方法体,被static修饰的可以用类名.调用,但是类名.调用抽象方法没有意义。
    b) abstract 和 final:被abstract修饰的方法强制子类重写,被final修饰的不让子类重写,所以其是矛盾的。
    c) abstract和private:被abstract修饰的是为了让子类看到并强制重写,被private修饰不让子类访问,所以他俩是矛盾的。
  2. 面向对象(接口的概述和特点)
    (1) 接口概述
    从狭义的角度讲就是指java中的interface
    从广义的角度讲对外提供规则的都是接口
    (2) 接口特点
    a. 接口用关键字interface表示
    interface 接口名 {}
    b. 类实现接口用implements表示
    class 类名 implements 接口名 {}
    c. 接口不能实例化
    那么,接口如何实例化呢?
    按照多态的方式实例化。
    d. 接口的子类
    可以是抽象类。但是意义不大。
    可以是具体类。要重写接口中的所有抽象方法。
    (3) 案例演示
    接口特点
class Demo1_Interface {
	public static void main(String[] args) {
		//Inter i = new Inter(); //接口不能被实例化,因为调用抽象方法没有意义。
		Inter i = new Demo();  //用多态方法实例化,父类引用指向子类对象
		i.print();
	}
}
interface Inter { 
	public abstract void print(); //接口中的方法都是抽象的
}
class Demo implements Inter {
	public void print() {
		System.out.println("print")}
}
  1. 面向对象(接口的成员特点)
    (1) 接口成员的特点:
    成员变量:只能是常量,并且是静态的并公用的。
    默认修饰符:public static final
    建议:自己动手给出
    构造方法:接口没有构造方法。
    成员方法:只能是抽象方法。
    默认修饰符:public abstract
    建议:自己手动给出。
    (2) 案例演示:接口成员特点
class Demo1_Interface {
	public static void main(String[] args) {
		Demo d = new Demo;
		d.print();
		System.out.println(Inter.num);
	}
}
interface Inter { 
	public static final int num = 10;//public static final 关键字是默认的,默认为静态常量
	//public Inter() {}  接口中没有构造方法

	/*public void print() {  接口不能定义非抽象方法
	}*/

	public abstract void print();
}
class Demo /*extends Object*/ implements Inter { //一个类不写继承类,则默认集成Object类
	public void print() {
		System.out.println(num)}
}

  1. 面向对象(类与类,类与接口,接口与接口的关系)、
    (1) 类与类
    继承关系,只能单继承,可以多层继承
    (2) 类与接口
    实现关系,可以单实现,也可以多实现
    并且还可以在继承一个类的同时实现多个接口。
class Demo1_Interface {
	public static void main(String[] args) {
		Demo d = new Demo;
		d.print();
		System.out.println(Inter.num);
	}
}
interface InterA {
	public abstract void printA();
}
interface InterB {
	public abstract void pritB();
}

//class Demo implements InterA,implements InterB { //这么做是不允许的,是非法的
class Demo extends Object implements InterA,InterB {
	public void printA() {
		System.out.println("printA");
	}
	public void printB() {
		System.ut.println("printB");
	}
}

(3) 接口与接口
继承关系,可以单继承,也可以多继承。

class Demo1_Interface {
	public static void main(String[] args) {
		Demo d = new Demo;
		d.print();
		System.out.println(Inter.num);
	}
}
interface InterA {
	public abstract void printA();
}
interface InterB {
	public abstract void pritB();
}
interface InterC extends InterB,InterA{
}

(4) 案例演示

  1. 面向对象(抽象类接口和接口的区别)
    (1) 成员区别
    抽象类:
    成员变量:可以变量,也可以常量
    构造方法:有
    成员方法:可以抽象,也可以非抽象
    接口:
    成员变量:只可以常量
    成员方法:只可以抽象。
    (2) 关系区别
    类与类:继承,单继承
    类与接口:实现,单实现,多实现
    接口与接口:继承,单继承,多继承
    (3) 设计理念与区别
    抽象类:被继承体现的是:“is a”的关系。抽象类中定义的是该继承体系的共性功能。
    接口:被实现体现的是:“like a”的关系,接口中定义的是该继承体系的扩展功能
  2. 面向对象(猫狗案例加入跳高功能分析及其代码实现)
    动物类:姓名,年龄,吃饭,睡觉
    猫和狗
    动物培训接口:跳高
class Test1_Animal {
	public static void main(String[] args) {
	 Cat c = new Cat("加菲"8);
	 c.eat();
	 c.sleep();

	 JumpCat jc = new JumpCat("跳高猫",3);
	  jc.eat();
	  jc.sleep();
	  jc.jump();
	}
}
abstract class Animal {
	private String name;
	private int age;

	public Animal() {}

	public Animal(String name,int age) {
		this.name = name;
		this.age = age;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public void getAge() {
		return age;
	}
	public abstract void eat() ;
	public abstract void sleep();
}
interface Jumping {
	public void jump();
}
class Cat extends Animal {
	public Cat() {}

	public Cat(String name,int age) {
		super(name,age);
	}
	public void eat() {
		System.out.printlin("猫吃鱼");
	}
	public void sleep(){
		System.out.println("侧着睡");
	}
}
class JumpCat extends Cat implements Jumping {
	public JumpCat() {}

	public JumpCat(String name,int age) {
		super(name,age);
	}
	public void jump() {
		System.out.println("猫跳高");
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值