JAVA中怎么继承interfere_Java学习笔记之:Java 继承

一、引言

继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类。继承可以理解为一个对象从另一个对象获取属性的过程。

如果类A是类B的父类,而类B是类C的父类,我们也称C是A的子类,类C是从类A继承而来的。在Java中,类的继承是单一继承,也就是说,一个子类只能拥有一个父类

继承中最常使用的两个关键字是extends和implements。

这两个关键字的使用决定了一个对象和另一个对象是否是IS-A(是一个)关系。

通过使用这两个关键字,我们能实现一个对象获取另一个对象的属性。

所有Java的类均是由java.lang.Object类继承而来的,所以Object是所有类的祖先类,而除了Object外,所有类必须有一个父类。

我们可以通过extends关键字可以申明一个类是继承另外一个类而来的,格式如下:

//父类

classFather {privateString name;private intage;public voidball() {

}

}//子类

class Son extendsFather {

}

二、实例

1.在父类的ball()方法中添加如下语句:

System.out.println("我会打球");

2.新建一个类,添加main函数,在函数中实例化Son类

public static voidmain(String[] args) {

Son son=newSon();

}

3.当我们打出“Son.”的时候,智能提示会显示可以使用的方法,由于Son类继承了Father类,所以我们可以直接使用Father类里面的方法。

8e940b2334968f2b060beaa14a3dd8e1.png

最后输出结果:

ee8055be6be863a7f045459d90dac517.png

4.当然你也可以在子类中添加自己的方法,如果想要使用直接实例化之后调用即可

class Son extendsFather {public voidSing() {

System.out.println("我会唱歌");

}

}

public static voidmain(String[] args) {

Son son=newSon();

son.ball();

son.Sing();

}

ad5c2720686c5b50a87d5b9b24ecde1d.png

三、函数重写

在Java中,子类可继承父类中的方法,而不需要重新编写相同的方法。但有时子类并不想原封不动地继承父类的方法,而是想作一定的修改,这就需要采用方法的重写。

在继承的关系中,子类的方法名和父类的方法名相同,返回值类型相同,参数列表相同,叫做方法重写。

1.只需要在子类中调用父类方法,编译器会自动生成重写的函数。

@Overridepublic voidball() {//TODO 自动生成的方法存根//super://1.作用就是用来子类调用父类的方法//2.如果实在构造方法中使用super,那么表示调用的是父类的构造方法。必须在第一行//3.必须在子类构造方法中才能调用父类构造方法

super.ball();

}

2.然后我们就可以在里面写自己想要的代码,在main函数实例化之后执行ball方法,结果

3d67db1d3832273c464a6dfd3024acb8.png

三、笔记

packagecom.hgd.study5;/*** 继承:一个类继另外一个类,被继承的类称之为父类或者基类,继承的类叫做子类或者派生类 子类继承父类,将继承父类的所有属性

* 子类继承父类,将继承父类的所属性和方法包含私有,但是不能访问,其他的都可以直接访问。子类具有拓展的功能

*

* 继承的作用:减少代码的冗余,提高了代码的重复利用

*

* 创建子类对象必先创建父类对象

*

* 方法重写的前提条件

* 方法重写:在继承关系中,子类的方法名和父类的方法名相同,而且需要返回值类型相同,参数列表相同,叫方法的重写

*

*@authorHuTiger

**/

public classInherit {public static classFather {privateString nameString;intage;publicFather() {//TODO 自动生成的构造函数存根

System.out.println("Father");

}public Father(inti) {//TODO 自动生成的构造函数存根

System.out.println("Father="+i);

}public intgetAge() {returnage;

}publicString getNameString() {returnnameString;

}public void setAge(intage) {this.age =age;

}public voidsetNameString(String nameString) {this.nameString =nameString;

}public voidball() {

System.out.println("我是" + getNameString() + "我会打球");

}

}public static class Son extendsFather {publicSon() {//TODO 自动生成的构造函数存根

super();//默认是存在的,而且必须是第一行,因为是调用父类的构造方法,父类必须先创建

System.out.println("Son");

}publicSon(String name) {//TODO 自动生成的构造函数存根

this();

System.out.println("Son="+name);

}public Son(inti) {//TODO 自动生成的构造函数存根

this();//表示调用了第一个构造方法

System.out.println("Son="+i);

}/** 子类扩展的技能*/

private voidsing() {

System.out.println("我是" + getNameString() + "我会唱歌");

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Java编写的俄罗斯方块的示例代码,你可以在Eclipse导入并直接运行它。该示例,你可以通过调整方块的方向、左右移动方块以及快速落下方块来进行游戏。当一行方块被消除时,你将获得分数,消除多行方块将获得更多分数。当你达到一定分数后,游戏将自动加速。你还可以暂停游戏。 ```java import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension;import java.awt.Graphics; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList; import java.util.Random; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class Tetris extends JFrame implements KeyListener { private static final long serialVersionUID = 1L; private final Point[][][] Tetraminos = { // I-Piece { { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) }, { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) }, { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) }, { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) } }, // J-Piece { { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 0) }, { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 2) }, { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 2) }, { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 0) } }, // L-Piece { { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 2) }, { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 2) }, { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 0) }, { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 0) } }, // O-Piece { { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) }, { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) }, { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) }, { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) } }, // S-Piece { { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) }, { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) }, { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) }, { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) } }, // T-Piece { { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(2, 1) }, { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) }, { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(1, 2) }, { new Point(1, 0), new Point(1, 1), new Point(2, 1), new Point(1, 2) } }, // Z-Piece { { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, { new Point(1, 1), new Point(0, 2), new Point(1, 2), new Point(0, 3) }, { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, { new Point(1, 1), new Point(0, 2), new Point(1, 2), new Point(0, 3) } } }; private final Color[] tetraminoColors = { Color.cyan, Color.blue, Color.orange, Color.yellow, Color.green, Color.pink, Color.red }; private Point pieceOrigin; private int currentPiece; private int rotation; private ArrayList<Integer> nextPieces = new ArrayList<Integer>(); private long score; private Color[][] well; // Creates a border around the well and initializes the dropping piece private void init() { well = new Color[12][24]; for (int i = 0; i < 12; i++) { for (int j = 0; j < 23; j++) { if (i == 0 || i == 11 || j == 22) { well[i][j] = Color.GRAY; } else { well[i][j] = Color.BLACK; } } } newPiece(); } // Put a new, random piece into the dropping position public void newPiece() { pieceOrigin = new Point(5, 2); rotation = 0; if (nextPieces.isEmpty()) { Collections.addAll(nextPieces, 0, 1, 2, 3, 4, 5, 6); Collections.shuffle(nextPieces); } currentPiece = nextPieces.get(0); nextPieces.remove(0); } // Collision test for the dropping piece private boolean collidesAt(int x, int y, int rotation) { for (Point p : Tetraminos[currentPiece][rotation]) { if (well[p.x + x][p.y + y] != Color.BLACK) { return true; } } return false; } // Rotate the piece clockwise or counterclockwise public void rotate(int i) { int newRotation = (rotation + i) % 4; if (newRotation < 0) { newRotation = 3; } if (!collidesAt(pieceOrigin.x, pieceOrigin.y, newRotation)) { rotation = newRotation; } repaint(); } // Move the piece left or right public void move(int i) { if (!collidesAt(pieceOrigin.x + i, pieceOrigin.y, rotation)) { pieceOrigin.x += i; } repaint(); } // Drops the piece one line or fixes it to the well if it can't drop public void dropDown() { if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) { pieceOrigin.y += 1; } else { fixToWell(); } repaint(); } // Make the dropping piece part of the well, so it is available for // checking for lines and doesn't interfere with the next piece public void fixToWell() { for (Point p : Tetraminos[currentPiece][rotation]) { well[pieceOrigin.x + p.x][pieceOrigin.y + p.y] = tetraminoColors[currentPiece]; } clearRows(); newPiece(); } public void deleteRow(int row) { for (int j = row-1; j > 0; j--) { for (int i = 1; i < 11; i++) { well[i][j+1] = well[i][j]; } } } // Clear completed rows from the field and award score according to // the number of simultaneously cleared rows. public void clearRows() { boolean gap; int numClears = 0; for (int j = 21; j > 0; j--) { gap = false; for (int i = 1; i < 11; i++) { if (well[i][j] == Color.BLACK) { gap = true; break; } } if (!gap) { deleteRow(j); j += 1; numClears += 1; } } switch (numClears) { case 1: score += 100; break; case 2: score += 300; break; case 3: score += 500; break; case 4: score += 800; break; } } // Draw the falling piece private void drawPiece(Graphics g) { g.setColor(tetraminoColors[currentPiece]); for (Point p : Tetraminos[currentPiece][rotation]) { g.fillRect((p.x + pieceOrigin.x) * 26, (p.y + pieceOrigin.y) * 26, 25, 25); } } @Override public void paintComponent(Graphics g) { // Paint the well g.fillRect(0, 0, 26*12, 26*23); for (int i = 0; i < 12; i++) { for (int j = 0; j < 23; j++) { g.setColor(well[i][j]); g.fillRect(26*i, 26*j, 25, 25); } } // Display the score g.setColor(Color.WHITE); g.drawString("" + score, 19*12, 25); // Draw the currently falling piece drawPiece(g); } public static void main(String[] args) { JFrame f = new JFrame("Tetris"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(12*26+10, 26*23+25); f.setVisible(true); final Tetris game = new Tetris(); game.init(); f.add(game); // Keyboard controls f.addKeyListener(new KeyListener() { public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_UP: game.rotate(-1); break; case KeyEvent.VK_DOWN: game.dropDown(); game.score += 1; break; case KeyEvent.VK_LEFT: game.move(-1); break; case KeyEvent.VK_RIGHT: game.move(+1); break; case KeyEvent.VK_SPACE: game.dropDown(); game.score += 2; break; case KeyEvent.VK_P: game.pause(); break; case KeyEvent.VK_ENTER: game.start(); break; } } public void keyReleased(KeyEvent e) { } }); // Make the falling piece drop every second new Thread() { @Override public void run() { while (true) { try { Thread.sleep(1000); game.dropDown(); } catch ( InterruptedException e ) {} } } }.start(); } public void pause() { JOptionPane.showMessageDialog(this, "Paused"); } public void start() { // TODO Auto-generated method stub } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } } --相关问题--:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值