java基础回顾-day21(动漫拼图实例)

这篇博客探讨了Java中的继承概念,作为面向对象编程的三大特性之一。通过继承,子类能够继承父类的属性和方法,并可进行重定义或扩展。文中提供了继承的格式示例,并通过一个动漫拼图的实例来加深理解,展示了继承如何提高代码的复用性。
摘要由CSDN通过智能技术生成

继承

概述

  • 继承是面向对象三大特征之一(封装,继承和多态)
  • 可以使得子类具有父类的属性和方法,还可以在子类中重新定义,追加属性和方法。通过继承,可以把父类中能够被访问到的成员变量和成员方法拿过来直接使用。
    继承的格式:
  • 格式:public class 子类名 extends 父类名 { }
  • 范例:public class Zi extends Fu { }
  • Fu:是父类,也被称为基类、超类
  • Zi:是子类,也被称为派生类
    简介
  • Zi类和Fu类,通过extends就产生了继承关系。这样呢,Zi类就可以使用Fu类中的成员了。
  • Fu这个类,被称为是父类,也被称为基类、超类,
  • Zi这个类:是子类,也被称为派生类。

继承的好处之一是:提高了代码的复用性

继承的练习

使用继承的方式,改写用户登录界面展示的案例

package demo;

import javax.swing.*;
import java.awt.*;

public class UserLoginFrame extends JFrame {
   
   public UserLoginFrame(){
   
//       初始化窗体
      initFrame();
//      绘制窗体
      paintView();
//      显示窗体
      this.setVisible(true);
   }
   public void paintView(){
   
//       显示用户名文本
      JLabel usernameLable = new JLabel("用户名");
      usernameLable.setBounds(50,50,50,20);
      this.add(usernameLable);
//      用户输入框
      JTextField usernameField = new JTextField();
      usernameField.setBounds(150,50,180,20);
      this.add(usernameField);
//      显示密码文本
      JLabel passwordLable = new JLabel("用户名");
      passwordLable.setBounds(50,100,50,20);
      this.add(passwordLable);
//      用户输入框
      JTextField passwordField= new JTextField();
      passwordField.setBounds(150,100,180,20);
      this.add(passwordField);
//      登录按钮
      JButton loginButton = new JButton("登录");
      loginButton.setBounds(50,200,280,20);
      this.add(loginButton);

   }
   public void initFrame(){
   
      this.setTitle("用户登录");
      this.setSize(400,300);
      this.setDefaultCloseOperation(3);
      this.setLocationRelativeTo(null);
      this.setAlwaysOnTop(true);
      this.setLayout(null);
   }
}

package demo;

public class App {
   
    public static void main(String[] args) {
   
        UserLoginFrame userLoginFrame = new UserLoginFrame();
    }
}

动漫拼图终品

package com.itheima;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class PictureFrame extends JFrame {
   
    private int[][] datas = {
   
            {
   1, 2, 3, 4},
            {
   5, 6, 7, 8},
            {
   9, 10, 11, 12},
            {
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值