Day 5 of submarine game:
Day 1 of submarine game:
- Design 6 classes, design and test the World class
Day 2 of submarine game:
- Add constructor to 6 classes and test
Day 3 of submarine game:
- Design SeaObject superclass, and six classes inherit SeaObject
- Two construction methods are designed for SeaObject, and six derived classes are called respectively
- Design submarine array, mine array and deep-water bomb array, and test them
Day 4 of submarine game:
- Override move() in 6 classes
- Add an access control modifier to a member in a class
- Design Images image class
Day 5 of submarine game:
-
Design the width and height of the window as constants and modify them where appropriate
-
Drawing window: in the World class - 3 steps in total, no need to master, Ctrl C/V
- import JFrame and JPanel
- Design the World class to inherit JPanel
- CV Dafa
-
Draw an ocean map, draw an object
1)If you want to draw an object, you need to obtain the image of the object. Each object can obtain the image, It means that the behavior of obtaining pictures is a common behavior, so the design is in SeaObject In, The behavior of each object to obtain pictures is different, so it is designed as an abstract method ----stay SeaObject Medium design getImage()Get a picture of the object 2)Overridden in 6 derived classes getImage()Get a picture of the object ----rewrite getImage() 3)Because only living objects need to be drawn into the window, the state of the object needs to be designed, Each object has a state, which means that the state is a common attribute, so the design is in SeaObject In, The state is generally designed as a constant and designed at the same time state Variable represents the current state ----stay SeaObject Medium design LIVE,DEAD Constant, state Variable represents the current state In the later business, it is also necessary to judge the state, and each object must judge the state, It means to judge that the state behavior is a common behavior, so the design is based on SeaObject In, The behavior of each object to judge the state is the same, so it is designed as a common method ----stay SeaObject Medium design isLive(),isDead()Judge the state of the object 4)Once you have all the data, you can start painting. Every object can be painted, It means that the behavior of the painting object is a common behavior, so the design is SeaObject In, The behavior of each object is the same, so it is designed as a common method ----stay SeaObject Medium design paintImage()Painting object 5)The behavior of the painting object is done in the window World Call in: 5.1)Prepare object 5.2)rewrite paint()Painting method
Review:
-
Method override: rewrite and overwrite
- Occurs in parent-child classes, with the same method name and the same parameter list
- When overriding a method is called, it depends on the type of object
The derived class thinks that the behavior of the superclass is not good enough. If you want to modify it, you need to override it
-
The difference between rewriting and overloading:
- Override: occurs in parent and child classes, with the same method name and parameter list
- Overload: occurs in the same class, with the same method name and different parameter lists
-
package and import:
-
Access control modifier: realize encapsulation, exposure and hiding ---- protect the security of data
- Public: public, any class
- Protected: protected, including this class, derived class and the same package class
- Default: write nothing. This class and the same package class ------------- java is not recommended
- Private: private, this class
-
Static: static
-
Static variable: static, which belongs to the class, method area, a copy, and the class name point
Data shared by all objects (pictures, audio, video, etc.)
-
Static method: static, which belongs to the class, method area, a copy, and the class name point
Static methods do not have implicit this pass and cannot directly access instance members
The operation of the method is object independent
-
Static block: static, which belongs to a class. It is automatically executed during class loading, once
Initialize / load static resources (pictures, audio, video, etc.)
-
Notes:
-
Final: final and unchangeable ----------- low probability of application alone
-
Modify variable: variable cannot be changed
//Demonstrate final modifier variables class Aoo{ final int num = 5; void show(){ //num = 55; // Compilation error, final variable cannot be changed } }
-
Modifier method: method cannot be overridden
//Demonstrate the final modifier class Boo{ final void show(){} } class Coo extends Boo{ //void show() {} / / compilation error. final modified methods cannot be overridden }
-
Decorated class: class cannot be inherited
//Demonstrate the final modifier class final class Doo{} //class Eoo extends Doo{} / / compilation error. final classes cannot be inherited class Foo{} final class Goo extends Foo{} //You can't be a father, but you can be a son
-
-
static final constant: high application rate
- Must declare simultaneous initialization
- It is accessed through the class name point and cannot be changed
- Suggestion: all letters of constant name are capitalized, and multiple words are used_ separate
- The compiler will directly replace constants with specific values during compilation, which is efficient
- When to use: data is always the same and often used
public class StaticFinalDemo { public static void main(String[] args) { System.out.println(Hoo.PI); //Access through class name points //Hoo.PI = 3.1415926; // Compilation error, constant cannot be changed //1) Load ioo Class into the method area //2) Store the static variable num in the method area together //3) Get the value of num in the method area and output it System.out.println(Ioo.num); //The compiler directly replaces constants with specific values during compilation, which is efficient //Equivalent to system out. println(5); System.out.println(Ioo.COUNT); } } class Ioo{ public static int num = 5; //Static variable public static final int COUNT = 5; //constant } class Hoo{ public static final double PI = 3.14159; //public static final int NUM; // Compilation error, constants must be declared and initialized at the same time }
-
Abstract method:
- Modified by abstract
- There is only the definition of methods, no specific implementation (not even {})
-
Abstract class:
-
Modified by abstract
-
The class containing abstract methods must be abstract
-
Abstract class cannot be instantiated (new object)
-
Abstract classes need to be inherited. Derived classes:
- Override all abstract methods ------------ change incomplete to complete
- It is also declared as an abstract class ----------------- generally not
-
Meaning of abstract class:
-
Encapsulating common attributes and behaviors ----------------- code reuse
-
Provide a unified type for all derived classes ----------- upward modeling code reuse
-
It can contain abstract methods to provide a unified entry for all derived classes (you can click it out)
The behavior of derived classes is different, but the entry is consistent, and it is equivalent to defining a standard
-
-
Essence notes:
-
Final: final and unchangeable ----------- low probability of application alone
- Modify variable: variable cannot be changed
- Modifier method: method cannot be overridden
- Decorated class: class cannot be inherited
-
static final constant: high application rate
- Must declare simultaneous initialization
- It is accessed through the class name point and cannot be changed
- Suggestion: all letters of constant name are capitalized, and multiple words are used_ separate
- The compiler will directly replace constants with specific values during compilation, which is efficient
- When to use: data is always the same and often used
-
Abstract method:
- Modified by abstract
- There is only the definition of methods, no specific implementation (not even {})
-
Abstract class:
-
Modified by abstract
-
The class containing abstract methods must be abstract
Classes that do not contain abstract methods can also be declared as abstract classes ------------ understand
-
Abstract class cannot be instantiated (new object)
-
Abstract classes need to be inherited. Derived classes:
- Override all abstract methods ------------ change incomplete to complete
- It is also declared as an abstract class ----------------- generally not
-
Meaning of abstract class:
-
Encapsulating common attributes and behaviors ----------------- code reuse
-
Provide a unified type for all derived classes ----------- upward modeling code reuse
-
It can contain abstract methods to provide a unified entry for all derived classes (you can click it out)
The behavior of derived classes is different, but the entry is consistent, and it is equivalent to defining a standard
-
-
Supplement:
-
Design rules:
-
Draw the properties and behaviors shared by derived classes into superclasses ------------ draw commonalities
-
If the behavior of derived classes is the same, they are designed as ordinary methods
If the behavior of the derived class is different, it is designed as an abstract method
-
------------Next Tuesday
-
-
Questions about abstract methods / classes:
-
What is the meaning of abstract methods?
- Ensure that when upward modeling occurs, the method can be pointed out through the reference of supertype
-
Since the meaning only lies in being able to point out, why not design it as an ordinary method?
- If it is designed as an ordinary method, the derived class can or can not be overridden. If it is designed as an abstract method, it can force the derived class to be overridden ----- as a standard, it must be overridden
-
Object oriented day 5 standard job:
List:
1. In the World class, the width and height of the design window are constant, and the data is modified to constant in the two parameter constructs of SeaObject
2. Drawing window: no need to master, CV can be used
3. Drawing ocean map and object:
- design the abstract method getImage() in the superclass and override it in the derived class
- design the state constant and current state variable in the superclass, and design isLive() and isDead() to judge the state
- Design paintImage() in the superclass to draw pictures
- override paint() in main and call the paintImage() method
1. In the World class, the width and height of the design window are constant, and the data is modified to constant in the two parameter constructs of SeaObject.. Reference code:
package day05; //The whole game world public class World { public static final int WIDTH = 641; public static final int HEIGHT = 479; public static void main(String[] args) { SeaObject[] submarines = new SeaObject[5]; //Submarine (reconnaissance submarine, torpedo submarine, mine submarine) submarines[0] = new ObserveSubmarine(); submarines[1] = new ObserveSubmarine(); submarines[2] = new TorpedoSubmarine(); submarines[3] = new TorpedoSubmarine(); submarines[4] = new MineSubmarine(); for(int i=0;i<submarines.length;i++){ System.out.println(submarines[i].x+","+submarines[i].y); submarines[i].move(); } Battleship s = new Battleship(); Mine[] ms = new Mine[2]; ms[0] = new Mine(100,200); ms[1] = new Mine(200,400); for(int i=0;i<ms.length;i++){ System.out.println(ms[i].x+","+ms[i].y); ms[i].move(); } Bomb[] bs = new Bomb[2]; bs[0] = new Bomb(100,200); bs[1] = new Bomb(200,400); for(int i=0;i<bs.length;i++){ System.out.println(bs[i].x+","+bs[i].y); bs[i].move(); } } } package day05; import java.util.Random; //Ocean object public class SeaObject { protected int width; protected int height; protected int x; protected int y; protected int speed; public SeaObject(int width,int height){ this.width = width; this.height = height; x = -width; Random rand = new Random(); y = rand.nextInt(World.HEIGHT-height-150)+150; speed = rand.nextInt(3)+1; } public SeaObject(int width,int height,int x,int y,int speed){ this.width = width; this.height = height; this.x = x; this.y = y; this.speed = speed; } public void move(){ System.out.println("Ocean object movement"); } } //Note: there are no changes in other categories, which are omitted here
2. Draw a window.. Reference code:
package day05; import javax.swing.JFrame; import javax.swing.JPanel; //The whole game world public class World extends JPanel { public static final int WIDTH = 641; public static final int HEIGHT = 479; public static void main(String[] args) { JFrame frame = new JFrame(); World world = new World(); world.setFocusable(true); frame.add(world); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(WIDTH+16, HEIGHT+39); frame.setLocationRelativeTo(null); frame.setVisible(true); } } //Note: there are no changes in other categories, which are omitted here
3. Drawing ocean map and object:
- design the abstract method getImage() in the superclass and override it in the derived class
- design the state constant and current state variable in the superclass, and design isLive() and isDead() to judge the state
- Design paintImage() in the superclass to draw pictures
- override paint() in main and call the paintImage() method
Reference code:
package day05; import javax.swing.ImageIcon; import java.awt.Graphics; import java.util.Random; //Ocean object public abstract class SeaObject { public static final int LIVE = 0; public static final int DEAD = 1; protected int state = LIVE; //current state protected int width; protected int height; protected int x; protected int y; protected int speed; public SeaObject(int width,int height){ this.width = width; this.height = height; x = width; Random rand = new Random(); y = rand.nextInt(World.HEIGHT-height-150)+150; speed = rand.nextInt(3)+1; } public SeaObject(int width,int height,int x,int y,int speed){ this.width = width; this.height = height; this.x = x; this.y = y; this.speed = speed; } public abstract void move(); public abstract ImageIcon getImage(); public boolean isLive(){ return state==LIVE; } public boolean isDead(){ return state==DEAD; } public void paintImage(Graphics g){ if(isLive()){ this.getImage().paintIcon(null,g,this.x,this.y); } } } package day05; import javax.swing.ImageIcon; //warship public class Battleship extends SeaObject { private int life; public Battleship(){ super(66,26,270,124,20); life = 5; } public void move(){ System.out.println("Warship movement"); } public ImageIcon getImage(){ return Images.battleship; } } package day05; import javax.swing.ImageIcon; //Reconnaissance submarine public class ObserveSubmarine extends SeaObject { public ObserveSubmarine(){ super(63,19); } public void move(){ System.out.println("Reconnaissance submarine x Move right"); } public ImageIcon getImage(){ return Images.obsersubm; } } package day05; import javax.swing.ImageIcon; //Torpedo submarine public class TorpedoSubmarine extends SeaObject { public TorpedoSubmarine(){ super(64,20); } public void move(){ System.out.println("Torpedo submarine x Move right"); } public ImageIcon getImage(){ return Images.torpesubm; } } package day05; import javax.swing.ImageIcon; //Mine submarine public class MineSubmarine extends SeaObject { public MineSubmarine(){ super(63,19); } public void move(){ System.out.println("Mine submarine x Move right"); } public ImageIcon getImage(){ return Images.minesubm; } } package day05; import javax.swing.ImageIcon; //mine public class Mine extends SeaObject { public Mine(int x,int y){ super(11,11,x,y,1); } public void move(){ System.out.println("mine y Move up"); } public ImageIcon getImage(){ return Images.mine; } } package day05; import javax.swing.ImageIcon; //Deep water bomb public class Bomb extends SeaObject { public Bomb(int x,int y){ super(9,12,x,y,3); } public void move(){ System.out.println("Deep water bomb y Move down"); } public ImageIcon getImage(){ return Images.bomb; } } package day05; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Graphics; //The whole game world public class World extends JPanel { public static final int WIDTH = 641; public static final int HEIGHT = 479; private Battleship ship = new Battleship(); //warship private SeaObject[] submarines = { new ObserveSubmarine(), new TorpedoSubmarine(), new MineSubmarine() }; //Submarine (reconnaissance submarine, torpedo submarine, mine submarine) private Mine[] mines = { new Mine(260,200) }; //mine private Bomb[] bombs = { new Bomb(200,190) }; //Deep water bomb public void paint(Graphics g){ Images.sea.paintIcon(null,g,0,0); //Draw an ocean map ship.paintImage(g); for(int i=0;i<submarines.length;i++){ submarines[i].paintImage(g); } for(int i=0;i<mines.length;i++){ mines[i].paintImage(g); } for(int i=0;i<bombs.length;i++){ bombs[i].paintImage(g); } } public static void main(String[] args) { JFrame frame = new JFrame(); World world = new World(); world.setFocusable(true); frame.add(world); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(WIDTH+16, HEIGHT+39); frame.setLocationRelativeTo(null); frame.setVisible(true); } } //Note: there are no changes in other categories, which are omitted here