//多态
public abstract class Animal {
public abstract void eat();
}
public class Cat extends Animal{
public void eat()
{
System.out.println("eat fish");
}
public void catchMouse()
{
System.out.println("catch mounse");
}
}
public class Dog extends Animal{
public void eat()
{
System.out.println("eat tone");
}
public void watch()
{
System.out.println("watch over");
}
}
public class Pig extends Animal {
public void eat()
{
System.out.println("eat food");
}
public void gongDi()
{
System.out.println("gong di");
}
}
/*
* 多态就可以理解为事物存在的多种体现形态
*
* 人:男人,女人
* 动物:猫,狗
* 猫 x = new 猫();
* 动物 x = new 动物();
* 1,多态的体现
* 父类的引用指向了自己的子类对象
* 父类的引用也可以接收自己的子类对象
* 2,多态的前提
* 必须是类与类之间有关系,要么继承,要么实现。
* 通常还有一个前提:存在覆盖
* 3,多态的好处
* 多态的出现大大的提高了程序的扩展性
* 4,多态的弊端
* 提高了扩展性,但是只能使用父类的引用访问父类中的成员
* 5,多态的应用
*
* 6,多态的出现代码中的特点(多态使用的注意事项)
*/
public class Welcome {
static final double PI = 3.14;
public static void main(String[] args) {
// TODO Auto-generated method stub
//Animal a = new Cat();//类型提升,向上转型
//a.eat();
//如果想要调用猫的特有方法时,该怎么做?
//强制将父类的引用,转成子类类型,向下转型
//Cat b = (Cat)a;
//b.catchMouse();
//千万不要出现这种操作,就是将父类对象转换成子类类型。
//我们能转换的是父类应用指向了自己的子类的对象时,该应用可以被提升,也可以被转换。
//多态自始至终都是子类对象在做着变化。
// Animal c = new Animal();
// Cat d = (Cat)c;
function(new Cat());
function(new Dog());
}
public static void function(Animal a)
{
a.eat();
if(a instanceof Cat)
{
Cat b = (Cat)a;
b.catchMouse();
}
else if(a instanceof Dog)
{
Dog b = (Dog)a;
b.watch();
}
else if(a instanceof Pig)
{
Pig b = (Pig)a;
b.gongDi();
}
}
}
public abstract class Student {
public abstract void study();
public void sleep()
{
System.out.println("lie sleep");
}
}
public class DoStudent {
public void doSome(Student stu)
{
stu.study();
stu.sleep();
}
}
public class AdStudent extends Student{
public void study()
{
System.out.println("ad study");
}
}
public class BaseStudent extends Student{
public void study()
{
System.out.println("bs study");
}
public void sleep()
{
System.out.println("sit sleep");
}
}
/*
* 基础班学生
* 学习,睡觉
* 高级班学生
* 学习,睡觉
* 可以将这两类事物进行抽取。
*/
public class Welcome {
public static void main(String[] args) {
// TODO Auto-generated method stub
DoStudent ds = new DoStudent();
ds.doSome(new BaseStudent());
ds.doSome(new AdStudent());
// BaseStudent bs = new BaseStudent();
// bs.study();
// bs.sleep();
// AdStudent as = new AdStudent();
// as.study();
// as.sleep();
}
}
public class MainPad{
public void usePCI(PCI p)//接口型引用指向自己的子类对象
{
if(p != null)
{
p.open();
p.close();
}
}
public void run()
{
System.out.println("mainpad run");
}
// public void useNetCard(NetCard c)
// {
// c.open();
// c.close();
// }
}
public interface PCI {
public void open();
public void close();
}
public class NetCard implements PCI{
public void open()
{
System.out.println("netcard open");
}
public void close()
{
System.out.println("netcard close");
}
}
public class SoundCard implements PCI {
public void open()
{
System.out.println("soundcard open");
}
public void close()
{
System.out.println("soundcard close");
}
}
/*
* 需求:
* 电脑运行实例
* 电脑运行基于主板
*/
public class Welcome {
public static void main(String[] args) {
// TODO Auto-generated method stub
MainPad mp = new MainPad();
mp.run();
mp.usePCI(new NetCard());
mp.usePCI(new SoundCard());
}
}
public interface UserInfoDao {
public void add(User user);
public void delete(User user);
}
public class UserInfoByJDBC implements UserInfoDao{
public void add(User user)
{
// 1,JDBC连接数据库
// 2,使用sql语句添加数据
// 3,关闭连接
}
public void delete(User user)
{
// 1,JDBC连接数据库
// 2,使用sql语句删除数据
// 3,关闭连接
}
}
public class UserInfoHibernate implements UserInfoDao {
public void add(User user)
{
// 1,hibernate连接数据库
// 2,使用sql语句添加数据
// 3,关闭连接
}
public void delete(User user)
{
// 1,hibernate连接数据库
// 2,使用sql语句删除数据
// 3,关闭连接
}
}
/*
* 需求:数据库的操作。
* 数据是:用户信息。
* 1,连接数据库 JDBC Hibernate
* 2,操作数据库
* c create r read u update d delete
* 3,关闭数据库连接
*
*/
public class Welcome {
public static void main(String[] args) {
// TODO Auto-generated method stub
//JOptionPane.showMessageDialog(null, "yes, it is.", "this is a reality!", JOptionPane.INFORMATION_MESSAGE);
UserInfoDao ui = new UserInfoJDBC();
ui.add(user);
ui.delete(user);
}
}