二十一集 对象的转型
主要内容这是一个非常非常的语法.就像吃饭喝水一样平常,面向对象多态性的一个体现.
1.对象的向上转型.
2.对象的向下转换.
对象的向上转型.
向上转型:将子类的对象赋值给父类的引用.
Student s = new Student();
Person p = s; //向上转换.子类的对象赋值给父类的引用.
向上转型是一定可以成功.
对象的向下转型:
向下转型--将父类的对象赋值给子类的引用 .
Student s1 = new Student();
Person p = s1;
Student s2 = (Student)p;
class Person20{
String name;
int age;
void introduce(){
System.out.println("我的名字"+ name+"年龄"+age);
}
}
class Student20 extends Person20{
String address;
void study(){
System.out.println("学习");
}
void introduce(){
//anotherFun();
super.introduce();//调用父类的成员函数,减少重复代码, 减少下一行代码
//System.out.println("我的姓名是"+name+"我的年龄是"+age);
System.out.println("我的家在"+address);
}
}
//一个引用能够调用那些成员(变量和函数),取决于这个引用的类型
//一个引用调用的是那一个方法,取决于这个引用指向的对象.
class Test20{
public static void main(String args[]){
//正确的向下转型
Person20 p = new Student20();
Student20 s = (Student20)p;
//错误的向下转型
//Person p = new Person();
//Student s = (student)p;
//向上转型
//Student20 s = new Student20();
//Person20 p = s;
//p.name = "张三";
//p.age = 20;
//p.address = "北京"; //此语句不能执行 person没有这个address
//一个引用能够调用那些成员(变量和函数),取决于这个引用的类型
//p.introduce();//调用子类的的introduce()
//一个引用调用的是那一个方法,取决于这个引用指向的对象.
}
}
第22集面向对象应用1
主要内容.
1.被客户不断变化的需求"折磨".
class Printer{
void open(){
System.out.println("open");
}
void close(){
System.out.println("close");
}
void print(String s){
System.out.println("print---->"+ s );
}
}
class HPPrinter extends Printer{
}
class CanonPrinter extends Printer{
void close(){
this.clean();
super.close();
}
void clean()
{
System.out.println("clean..");
}
}
class TestPrinter{
public static void main(String args[]){
int flag =1;
if( flag==0)
{
HPPrinter Hprinter = new HPPrinter();
Hprinter.open();
Hprinter.print("abcdefg");
Hprinter.close();
}
else if(flag==1)
{
CanonPrinter canonprinter = new CanonPrinter();
canonprinter.open();
canonprinter.print("abcdefg");
canonprinter.close();
}
}
}
主要内容
1.抽象函数的语法特征.
2.抽象类的语法特征.
3.抽象类的作用.
在面向对象有一个非常非常有用的东西,先抽象,再具体.
因为抽象不容易出现错误
·抽象类天生是当爹的
问1:什么是抽象函数
只有函数的定义,没有函数体的函数被称为抽象函数.
abstract void fun();
问2什么是抽象类
使用abstract定义的类被称之为抽象类.
1.抽象类不能够生成对象.
2.如果一个类当中包含有抽象函数,那么这个类必须声明为抽象类.
3.如果一个类当中没有抽象函数,那么这类也可以被声明为抽象类.
抽象类可以有构造函数么?
条件:
1.抽象类不能生成对象.
2.构造函数用于生成类的对象.
结论: 如果从这个片面的两点来看,抽象类没有构造函数. 但是.抽象类可以有构造函数,这个构造是为子类服务的.
abstract class Person23{
String name;
int age;
Person23(){
System.out.println("person构造函数");
}
Person23(String name ,int age){
this.name = name;
this.age = age;
}
void introduce(){
System.out.println("名字是" +name+age);
}
abstract void eat();
}
class Chinese extends Person23{
String address;
Chinese(){
System.out.println("chinese构造函数");
}
Chinese(String name,int age ,String address){
super(name,age);
this.address = address;
}
void eat(){
System.out.println("用筷子吃饭");
}
}
class TestChinese{
public static void main(String args[]){
Person23 p = new Chinese();
p.eat();
}
}
总结
1.抽象函数就是没有函数体的函数.
2.抽象类使用abstrace来定义.
3.抽象类不能生成对象,但是却可以拥有构造函数. 这构造函数是为了子类准备的.
24集 为什么用抽象类
主要内容
抽象类表达的是一种概念
//如果一段代码在语意上是有错误的,那么在语法上也应该是有错误的.
abstract class Printer{
void open(){
System.out.println("open");
}
void close(){
System.out.println("close");
}
void print(){
}
//abstract void print()//抽象函数
//在语意上有错误,我们让它在语法上也有错误.
//语法上的错误更加容易检查出来.
}
// 该打印机为喷墨打印机进行打印
class HPPrinter extends Printer{
void print(){
System.out.println("使用喷墨打印机进行打印");
}
}
//该打印机为针式打印机
class CanonPrinter extends Printer{
void print(){
System.out.println("使用针式打印机进行打印");
}
}
class Test{
public static void main(String args[]){
Printer p1 = new HPPrinter();//向上转型,子类对象赋给父类的引用.
p1.open();
p1.print();
p1.close();
Printer p2 = new CanonPrinter();//向上转型,子类对象赋给父类的引用.
p2.open();
p2.print();
p2.close();
}
}
如果不使用抽象类,子类就可能忘记实现父类的print()方法.
.