java提现功能_多态在Java中的提现思想

我们都知道在java中,面向对象有三大特征,封装,继承,和多态。封装隐藏实现细节,对外提供享用的访问方式,而且提高了代码的复用性。在事物对象有相同的属性和共同的内容的时候,我们可以把相同的内容的抽取出来,然后不同的事物类别去继承这些共同的属性。继承虽然打破了代码的封装,但是同样起到复用代码的好处。那我们再看看多态在java中是怎么体现的。

首先,我们来看一段代码,包括其中的备注和分析:

//描述狗,狗有吃饭,看家的行为

classDog

{public voideat()

{

System.out.println("啃骨头");

}public voidlookHome()

{

System.out.println("看家");

}

}//描述猫,猫有吃饭,抓老鼠行为

classCat

{public voideat()

{

System.out.println("吃鱼");

}public voidcatchMouse()

{

System.out.println("抓老鼠");

}

}

Class Demo

{public static voidmain(String[] args)

{/*有多个狗和猫对象都要调用吃饭这个行为

这样会导致d.eat();代码重复性非常严重

Dog d = new Dog();

d.eat();

为了提高代码的复用性,可以将d.eat();代码进行封装

public static void method(Dog d)

{

d.eat();

}

然后创建对象,直接调用method方法即可

method(new Dog());

但当创建Cat对象时,同样需要调用eat方法,同样可以将eat方法进行封装

public static void method(Cat c)

{

c.eat();

}*/}

}

如果当有了猪之类,创建猪这个对象,那么我们同样要封装eat的方法,每当多一种动物,都要单独定义功能,封装eat这个方法,让动物去吃,很明前代码的扩展性很差。我们分析发现不管是dog,cat还是还是如果后期出现的猪,eat是他们的共性,那么可以将eat这个动作进行抽取到父类中,如下:

abstract classAnimal

{abstract public voideat();

}

由于每一种动物的eat方式都不一样,因此在父类中无法准确描述eat的具体行为,所以只能使用抽象的方法进行描述,而这个类也为抽象类别。然后把Dog和Cat这个类继承Animal这个父类如下:

//描述狗,狗有吃饭,看家的行为

class Dog extendsAnimal

{public voideat()

{

System.out.println("啃骨头");

}public voidlookHome()

{

System.out.println("看家");

}

}//描述猫,猫有吃饭,抓老鼠行为//..........................................................................................

class Cat extendsAnimal

{public voideat()

{

System.out.println("吃鱼");

}public voidcatchMouse()

{

System.out.println("抓老鼠");

}

}

其次,我们可以分析,Dog和Cat都属于Animal的一种,那么我们是否可以引用Animal创建具体的对象呢?

Animal d = new Dog();

Animal c = new Cat();

在上述代码基础之上,当再让动物去做事时,不用面对具体的动物,而只要面对Animal即可。因此method方法可以修改为:

public static voidmethod(Animal a)

{

a.eat();

}

method(Animal a)可以接受Animal的子类型的所有小动物,而method方法不用在关心是具体的哪一个类型。即就是只建立Animal的引用就可以接收所有的Dog和Cat对象进来,让它们去eat。从而提高了程序的扩展性。其实,上述的代码已经形成多态了,Dog和Cat都是具体的动物,同时也是Animal的形态。多态顾名思义,一种事物有多种表现形态。

通过以上代码和解释,我们可以很简单的了解到多态在面向对象里面的思想。同时我们也应该清楚的认识到,用多态来描述一个事物,这个事物的前提是必须有继承的父类或者实现的接口。多态提高了程序的扩展性。然而,在上诉的例子中,Animal的引用指向Dog或者Cat的时候,只能使用父类中的方法eat,不能调用子类中特有的方法,比如Dog的lookhome()的功能。如果我们要访问子类特有的功能该怎么办呢?

当父类的引用指向子类对象时,就发生了向上转型,即把子类类型对象转成了父类类型。向上转型的好处是隐藏了子类类型,提高了代码的扩展性。当要使用子类特有的功能的时候,就要使用向下转型。

比如,继续上述的例子,当Dog的类对象d调用lookhome的功能的时候,我们就需要强制转化类型。但是向下转型的时候,我们必须要做类型的判断,判断对象是否是我们要转换类型的一个实例,否则会发生ClassCastException异常转化错误。如下:

If(d instanceof Dog){

Dog d1 = (Dog)d;

}

做完判断之后,如果是,我们就把对象d向下转型为Dog的类型并且赋值给d1,这样的

就可以调用Dog类中特有的方法d1.lookhome();

多态成员的变化:

体会多态的思想之后,那么我们再来想想,如果使用多态描述事物,那么类的成员有啥变化呢?

l  成员变量

我们看如下代码:

classFu

{int num = 4;

}class Zi extendsFu

{int num = 5;

}classDemo

{public static voidmain(String[] args)

{

Fu d= newZi();

System.out.println(d.num);

Zi z= newZi();

System.out.println(z.num);

}

}

运行看结果如下:

0ae173dab6bf3dd47e65209fdcfd2397.png

当子父类中出现同名的成员变量时,多态调用该变量时:

编译时期:参考的是引用型变量所属的类中是否有被调用的成员变量。没有,编译失败。

运行时期:也是调用引用型变量所属的类中的成员变量。

l  成员方法

如果是调用成员方法呢?看如下代码:

lass Fu

{int num = 4;voidshow()

{

System.out.println("Fu show num");

}

}class Zi extendsFu

{int num = 5;voidshow()

{

System.out.println("Zi show num");

}

}classDemo

{public static voidmain(String[] args)

{

Fu f= newZi();

f.show();

}

}

运行结果如下:

a05fb7d97c7ff1013d51cad4654a806e.png

多态成员函数

编译时期:参考引用变量所属的类,如果没有类中没有调用的函数,编译失败。

运行时期:参考引用变量所指的对象所属的类,并运行对象所属类中的成员函数。

l  静态方法

如果,是类中的被静态修饰的方法呢?

classFu

{int num = 4;static voidmethod()

{

System.out.println("fu static method run");

}

}class Zi extendsFu

{int num = 5;static voidmethod()

{

System.out.println("zi static method run");

}

}classDemo

{public static voidmain(String[] args)

{

Fu f= newZi();

f.method();

}

}

运行结果如下:

204697288768add575e18ece5f73bd97.png

多态静态函数

多态调用时:编译和运行都参考引用类型变量所属的类中的静态函数。

简而言之:编译和运行看等号的左边。其实真正调用静态方法是不需要对象的,静态方法通过类直接调用。

总结,在多态中,对于成员变量和静态方法,比哪一运行都看引用所属的类。

对于非静态的方法,比哪一时看引用所属的类中是否有这个方法,而运行的时候回到引用所指的对象中类中找方法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是用Java编写的一个简单的银行转账系统,包括取款,存款,转账等功能,其用到了数据库的连接,采用Eclipse编写,包含数据库的设计文件。非常适合有一定基础的Java初学者使用。 package com.gujunjia.bank; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.sql.*; /** * * @author gujunjia */ public class DataBase { static Connection conn; static PreparedStatement st; static ResultSet rs; /** * 加载驱动 */ public static void loadDriver() { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("加载驱动失败"); } } /** * 创建数据库的连接 * * @param database * 需要访问的数据库的名字 */ public static void connectionDatabase(String database) { try { String url = "jdbc:mysql://localhost:3306/" + database; String username = "root"; String password = "gujunjia"; conn = DriverManager.getConnection(url, username, password); } catch (SQLException e) { System.out.println(e.getMessage()); } } /** * 关闭数据库连接 */ public static void closeConnection() { if (rs != null) { // 关闭记录集 try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (st != null) { // 关闭声明 try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { // 关闭连接对象 try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } package com.gujunjia.bank; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * 本类主要实现整个系统的界面 * * @author gujunjia */ public class MainFrame extends JFrame implements ActionListener, FocusListener { /** * */ private static final long serialVersionUID = 1L; public static String userId; JTextField userIdText; JPasswordField passwordText; JButton registerButton; JButton logInButton; public MainFrame() { super("个人银行系统"); this.setSize(400, 500); this.setLocation(getMidDimension(new Dimension(400, 500))); getAppearance(); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /** * 获取屏幕的间尺寸 * * @param d * Dimension类型 * @return 一个Point类型的参数 */ public static Point getMidDimension(Dimension d) { Point p = new Point(); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); p.setLocation((dim.width - d.width) / 2, (dim.height - d.height) / 2); return p; } /** * 布局 * * @return Container */ public Container getAppearance() { Container container = this.getContentPane(); container.setLayout(new GridLayout(4, 0)); JLabel label1 = new JLabel("个人银行系统"); label1.setFont(new Font("楷体", Font.BOLD, 40)); JLabel label2 = new JLabel("账号:"); label2.setFont(new Font("楷体", Font.PLAIN, 15)); JLabel label3 = new JLabel("密码:"); label3.setFont(new Font("楷体", Font.PLAIN, 15)); userIdText = new JTextField(20); userIdText.addFocusListener(this); passwordText = new JPasswordField(20); passwordText.addFocusListener(this); JPanel jp1 = new JPanel(); JPanel jp2 = new JPanel(); JPanel jp3 = new JPanel(); JPanel jp4 = new JPanel(); jp1.add(label1); jp2.add(label2); jp2.add(userIdText); jp3.add(label3); jp3.add(passwordText); registerButton = new JButton("注册"); registerButton.addActionListener(this); registerButton.setFont(new Font("楷体", Font.BOLD, 15)); logInButton = new JButton("登录"); logInButton.addActionListener(this); logInButton.setFont(new Font("楷体", Font.BOLD, 15)); jp4.add(registerButton); jp4.add(logInButton); container.add(jp1); container.add(jp2); container.add(jp3); container.add(jp4); return container; } public void actionPerformed(ActionEvent e) { Object btn = e.getSource(); if (btn == registerButton) { new Register(); } else if (btn == logInButton) { String id = userIdText.getText().trim(); String password = new String(passwordText.getPassword()); Bank bank = new Bank(); if (id.equals("") || password.equals("")) { JOptionPane.showMessageDialog(null, "请输入账号和密码"); } else { String dPassword = bank.getPassword(id); if (password.equals(dPassword)) { userId = id; this.dispose(); new UserGUI(); } else { JOptionPane.showMessageDialog(this, "密码或用户名错误", "错误", JOptionPane.ERROR_MESSAGE); } } } } @Override public void focusGained(FocusEvent e) { Object text = e.getSource(); if (text == userIdText) { userIdText.setText(""); userIdText.setFont(new Font("宋体", Font.BOLD, 15)); } else if (text == passwordText) { passwordText.setText(""); } } @Override public void focusLost(FocusEvent e) { Object text = e.getSource(); if (text == userIdText) { if (userIdText.getText().equals("")) { userIdText.setText("请输入账号"); userIdText.setFont(new Font("楷体", Font.ITALIC, 15)); } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值