帮我解释这段代码public class Light : ISwitch { private string id; public string Id { get { return id; } } public Light(string _id) { this.id = _id; } public void On() { Console.WriteLine("电灯开了"); } public void Off() { Console.WriteLine("电灯关了"); } public int Myint() { return 0; } }
时间: 2024-02-14 07:30:47 浏览: 90
这段代码定义了一个名为 Light 的类,实现了 ISwitch 接口。下面是对代码的解释:
- `public class Light : ISwitch`:定义了一个公共的 Light 类,并实现了 ISwitch 接口。ISwitch 接口可能包含一些与开关相关的方法,该类需要实现这些方法。
- `private string id;`:声明了一个私有的字符串类型的 id 变量。
- `public string Id { get { return id; } }`:定义了一个公共的只读属性 Id,用于获取 id 变量的值。
- `public Light(string _id)`:定义了一个公共的构造函数,该构造函数接受一个字符串类型的参数 _id,用于初始化 id 变量。
- `public void On()`:定义了一个公共的无返回值的方法 On,该方法用于打开电灯。在方法体中,会输出 "电灯开了" 的信息。
- `public void Off()`:定义了一个公共的无返回值的方法 Off,该方法用于关闭电灯。在方法体中,会输出 "电灯关了" 的信息。
- `public int Myint()`:定义了一个公共的返回整数类型的方法 Myint,该方法返回值为 0。
总结起来,这段代码定义了一个电灯类 Light,该类具有打开和关闭电灯的功能,并且提供了获取电灯ID和返回整数的方法。
相关问题
java代码实现坦克大战
坦克大战是经典的游戏,以下是一个简单的 Java 代码实现:
```java
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class TankWar extends JFrame {
private static final long serialVersionUID = 1L;
// 窗口大小
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
private MyPanel mp;
public static void main(String[] args) {
new TankWar();
}
public TankWar() {
mp = new MyPanel();
this.add(mp);
this.setTitle("坦克大战");
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
}
private class MyPanel extends JPanel implements KeyListener {
private static final long serialVersionUID = 1L;
private Tank myTank;
private ArrayList<Tank> enemyTanks;
private ArrayList<Missile> missiles;
private ArrayList<Explode> explodes;
public MyPanel() {
myTank = new Tank(400, 500, true);
enemyTanks = new ArrayList<Tank>();
missiles = new ArrayList<Missile>();
explodes = new ArrayList<Explode>();
for (int i = 0; i < 5; i++) {
enemyTanks.add(new Tank(i * 150, 0, false));
}
this.setFocusable(true);
this.addKeyListener(this);
new Thread(new EnemyTankThread()).start();
new Thread(new MissileThread()).start();
new Thread(new ExplodeThread()).start();
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
myTank.draw(g);
for (int i = 0; i < enemyTanks.size(); i++) {
enemyTanks.get(i).draw(g);
}
for (int i = 0; i < missiles.size(); i++) {
missiles.get(i).draw(g);
}
for (int i = 0; i < explodes.size(); i++) {
explodes.get(i).draw(g);
}
g.setColor(Color.WHITE);
g.drawString("Missiles: " + missiles.size(), 10, 30);
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
myTank.move(Tank.UP);
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
myTank.move(Tank.DOWN);
} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
myTank.move(Tank.LEFT);
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
myTank.move(Tank.RIGHT);
} else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
missiles.add(myTank.fire());
}
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
private class EnemyTankThread implements Runnable {
@Override
public void run() {
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < enemyTanks.size(); i++) {
enemyTanks.get(i).move((int) (Math.random() * 4));
if (Math.random() < 0.2) {
missiles.add(enemyTanks.get(i).fire());
}
}
repaint();
}
}
}
private class MissileThread implements Runnable {
@Override
public void run() {
while (true) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < missiles.size(); i++) {
Missile missile = missiles.get(i);
if (missile.isAlive()) {
missile.move();
for (int j = 0; j < enemyTanks.size(); j++) {
Tank enemyTank = enemyTanks.get(j);
if (enemyTank.isAlive() && missile.hit(enemyTank)) {
explodes.add(new Explode(enemyTank.getX(), enemyTank.getY()));
enemyTank.setAlive(false);
missile.setAlive(false);
}
}
if (myTank.isAlive() && missile.hit(myTank)) {
explodes.add(new Explode(myTank.getX(), myTank.getY()));
myTank.setAlive(false);
missile.setAlive(false);
}
} else {
missiles.remove(i);
i--;
}
}
repaint();
}
}
}
private class ExplodeThread implements Runnable {
@Override
public void run() {
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < explodes.size(); i++) {
Explode explode = explodes.get(i);
if (explode.isAlive()) {
explode.nextStep();
} else {
explodes.remove(i);
i--;
}
}
repaint();
}
}
}
}
}
class Tank {
public static final int UP = 0;
public static final int DOWN = 1;
public static final int LEFT = 2;
public static final int RIGHT = 3;
private static final int SIZE = 30;
private static final int SPEED = 5;
private int x;
private int y;
private boolean good;
private boolean alive;
private int direction;
public Tank(int x, int y, boolean good) {
this.x = x;
this.y = y;
this.good = good;
this.alive = true;
this.direction = UP;
}
public void draw(Graphics g) {
if (alive) {
g.setColor(good ? Color.GREEN : Color.YELLOW);
g.fillOval(x, y, SIZE, SIZE);
g.setColor(good ? Color.DARK_GRAY : Color.LIGHT_GRAY);
switch (direction) {
case UP:
g.fillRect(x + SIZE / 2 - 5, y, 10, SIZE / 2);
break;
case DOWN:
g.fillRect(x + SIZE / 2 - 5, y + SIZE / 2, 10, SIZE / 2);
break;
case LEFT:
g.fillRect(x, y + SIZE / 2 - 5, SIZE / 2, 10);
break;
case RIGHT:
g.fillRect(x + SIZE / 2, y + SIZE / 2 - 5, SIZE / 2, 10);
break;
}
}
}
public void move(int direction) {
this.direction = direction;
switch (direction) {
case UP:
y -= SPEED;
if (y < 0) {
y = 0;
}
break;
case DOWN:
y += SPEED;
if (y > TankWar.HEIGHT - SIZE) {
y = TankWar.HEIGHT - SIZE;
}
break;
case LEFT:
x -= SPEED;
if (x < 0) {
x = 0;
}
break;
case RIGHT:
x += SPEED;
if (x > TankWar.WIDTH - SIZE) {
x = TankWar.WIDTH - SIZE;
}
break;
}
}
public Missile fire() {
int missileX = x + SIZE / 2 - Missile.SIZE / 2;
int missileY = y + SIZE / 2 - Missile.SIZE / 2;
return new Missile(missileX, missileY, direction, good);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean isAlive() {
return alive;
}
public void setAlive(boolean alive) {
this.alive = alive;
}
}
class Missile {
public static final int SIZE = 10;
public static final int SPEED = 10;
private int x;
private int y;
private boolean good;
private boolean alive;
private int direction;
public Missile(int x, int y, int direction, boolean good) {
this.x = x;
this.y = y;
this.direction = direction;
this.good = good;
this.alive = true;
}
public void draw(Graphics g) {
if (alive) {
g.setColor(good ? Color.BLUE : Color.RED);
g.fillOval(x, y, SIZE, SIZE);
}
}
public void move() {
switch (direction) {
case Tank.UP:
y -= SPEED;
break;
case Tank.DOWN:
y += SPEED;
break;
case Tank.LEFT:
x -= SPEED;
break;
case Tank.RIGHT:
x += SPEED;
break;
}
if (x < 0 || x > TankWar.WIDTH || y < 0 || y > TankWar.HEIGHT) {
alive = false;
}
}
public boolean hit(Tank tank) {
if (good == tank.good) {
return false;
}
if (x >= tank.getX() && x <= tank.getX() + Tank.SIZE && y >= tank.getY() && y <= tank.getY() + Tank.SIZE) {
return true;
}
return false;
}
public boolean isAlive() {
return alive;
}
public void setAlive(boolean alive) {
this.alive = alive;
}
}
class Explode {
private static final int SIZE = 50;
private static final int STEPS = 5;
private int x;
private int y;
private boolean alive;
private int step;
public Explode(int x, int y) {
this.x = x - SIZE / 2;
this.y = y - SIZE / 2;
this.alive = true;
this.step = 0;
}
public void draw(Graphics g) {
if (alive) {
Color color = g.getColor();
g.setColor(Color.ORANGE);
g.fillOval(x, y, SIZE - step * 10, SIZE - step * 10);
g.setColor(color);
nextStep();
}
}
public void nextStep() {
step++;
if (step == STEPS) {
alive = false;
}
}
public boolean isAlive() {
return alive;
}
}
```
这个代码实现了基本的游戏功能,包括坦克移动、开火、子弹碰撞、爆炸效果等。你可以尝试添加更多的功能来完善这个游戏。
阅读全文