坦克大战DEMO

Jul 25, 2016 am 09:08 AM

实现了基本功能....地方坦克寻路做得比较弱智,每个坦克最多能同时发射5发炮弹....敌方有三种兵种,菜单栏没做响应,这个很简单,需要的可以自己加......上传全部代码,仅供学习参考...... 坦克大战DEMO
  1. package tank.common;
  2. abstract public class Bullet implements Runnable, Common {
  3. private int x, y;
  4. private int speed;
  5. private int direction;
  6. private boolean alive;
  7. protected int power;
  8. public Bullet(int x, int y, int speed, int direction) {
  9. this.x = x;
  10. this.y = y;
  11. this.speed = speed;
  12. this.direction = direction;
  13. alive = true;
  14. }
  15. public void die() {
  16. alive = false;
  17. }
  18. public int getPower() {
  19. return power;
  20. }
  21. public int getX() {
  22. return x;
  23. }
  24. public int getY() {
  25. return y;
  26. }
  27. public boolean isAlive() {
  28. return alive;
  29. }
  30. public void run() {
  31. while (true) {
  32. try {
  33. Thread.sleep(15);
  34. } catch (InterruptedException e) {
  35. // TODO Auto-generated catch block
  36. e.printStackTrace();
  37. }
  38. switch (direction) {
  39. case UP:
  40. y -= speed;
  41. break;
  42. case DOWN:
  43. y += speed;
  44. break;
  45. case RIGHT:
  46. x += speed;
  47. break;
  48. case LEFT:
  49. x -= speed;
  50. break;
  51. }
  52. if (x WIDTH || y > HEIGHT || y alive = false;
  53. return;
  54. }
  55. }
  56. }
  57. }
复制代码
  1. package tank.common;
  2. public interface Common {
  3. public static final int UP = 0;
  4. public static final int DOWN = 1;
  5. public static final int RIGHT = 2;
  6. public static final int LEFT = 3;
  7. public static final int WIDTH = 600;
  8. public static final int HEIGHT = 400;
  9. public static final int WATER = 0;
  10. public static final int WALLS = 1;
  11. public static final int STEELS = 2;
  12. public static final int GRASS = 3;
  13. }
复制代码
  1. package tank.common;
  2. import java.awt.Color;
  3. import java.awt.Point;
  4. public abstract class EnemyTank extends Tank {
  5. private class AutoFire implements Runnable {
  6. @Override
  7. public void run() {
  8. // TODO Auto-generated method stub
  9. while (isAlive()) {
  10. try {
  11. Thread.sleep(500);
  12. } catch (InterruptedException e) {
  13. // TODO Auto-generated catch block
  14. e.printStackTrace();
  15. }
  16. if (Math.random() > shotKey) {
  17. shot();
  18. }
  19. }
  20. }
  21. }
  22. private class AutoMove implements Runnable {
  23. @Override
  24. public void run() {
  25. // TODO Auto-generated method stub
  26. int moves[] = new int[4];
  27. while (isAlive()) {
  28. try {
  29. Thread.sleep(110);
  30. } catch (InterruptedException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. for (int i = 0; i moves[i] = judgeHero(i);
  35. }
  36. int direction = 0;
  37. int max = Integer.MIN_VALUE;
  38. for (int i = 0; i if (moves[i] >= max) {
  39. max = moves[i];
  40. direction = i;
  41. }
  42. }
  43. move(direction);
  44. }
  45. }
  46. }
  47. private double shotKey;
  48. private Point heroPosition;
  49. private Tank hero;
  50. public EnemyTank(int x, int y, Tank hero, int life, int ID) {
  51. super(x, y, Color.cyan);
  52. this.lifes = life;
  53. this.hero = hero;
  54. super.setImageID(ID);
  55. }
  56. private int judgeHero(int direction) {
  57. heroPosition = new Point(hero.getX() + 9, hero.getY() + 9);
  58. int result = 0;
  59. int x = this.getX();
  60. int y = this.getY();
  61. int speed = this.getSpeed();
  62. double distance1 = Math.abs((this.getX() - heroPosition.x)
  63. * (this.getX() - heroPosition.x)
  64. + (this.getY() - heroPosition.y)
  65. * (this.getY() - heroPosition.y));
  66. switch (direction) {
  67. case UP:
  68. y -= speed;
  69. break;
  70. case DOWN:
  71. y += speed;
  72. break;
  73. case RIGHT:
  74. x += speed;
  75. break;
  76. case LEFT:
  77. x -= speed;
  78. break;
  79. }
  80. if (getDirection() == direction) {
  81. result += 5000;
  82. }
  83. if (!canMove(x, y)) {
  84. result -= Integer.MAX_VALUE;
  85. }
  86. double distance2 = Math.abs((x - heroPosition.x) * (x - heroPosition.x)
  87. + (y - heroPosition.y) * (y - heroPosition.y));
  88. if (Math.random() > 0.8) {
  89. result += Math.random() * 20000;
  90. }
  91. result += (distance1 - distance2) * 10;
  92. return result;
  93. }
  94. public void setPosition(int x, int y) {
  95. super.x = x;
  96. super.y = y;
  97. }
  98. public void setShotSpeed(double shotSpeed) {
  99. this.shotKey = shotSpeed;
  100. }
  101. public void startFire() {
  102. new Thread(new AutoFire()).start();
  103. }
  104. public void startMove() {
  105. new Thread(new AutoMove()).start();
  106. }
  107. }
复制代码
  1. package tank.common;
  2. import java.awt.Color;
  3. import java.util.ArrayList;
  4. import tank.entity.NormalBullet;
  5. public abstract class Tank implements Common {
  6. protected int x;
  7. protected int y;
  8. private Color color;
  9. private int speed;
  10. private int direction;
  11. private ArrayList bullets;
  12. private ArrayList tanks;
  13. private Bullet bullet;
  14. private int maxBulletNum;
  15. private boolean alive;
  16. protected int lifes;
  17. ArrayList walls;
  18. private int tankImageID;
  19. {
  20. speed = 2;
  21. direction = UP;
  22. alive = true;
  23. }
  24. public Tank() {
  25. this.x = 0;
  26. this.y = 0;
  27. color = Color.black;
  28. }
  29. public Tank(int x, int y, Color color) {
  30. this.x = x;
  31. this.y = y;
  32. this.color = color;
  33. maxBulletNum = 5;
  34. bullets = new ArrayList();
  35. walls = new ArrayList();
  36. }
  37. protected boolean canMove(int x, int y) {
  38. if (x WIDTH - 20 || y HEIGHT - 20) {
  39. return false;
  40. }
  41. if (tanks == null) {
  42. return true;
  43. }
  44. if (tanks.size() == 1) {
  45. return true;
  46. }
  47. for (int i = 0; i Wall tempWall = walls.get(i);
  48. if (tempWall.isAlive()) {
  49. if (x >= tempWall.getX() && y >= tempWall.getY()) {
  50. if (x return tempWall.canBeWalk();
  51. }
  52. } else if (x >= tempWall.getX() && y if (x && (y + 20) >= tempWall.getY()) {
  53. return tempWall.canBeWalk();
  54. }
  55. } else if (x = tempWall.getY()) {
  56. if ((x + 20) >= tempWall.getX()
  57. && y return tempWall.canBeWalk();
  58. }
  59. } else if (x if ((x + 20) >= tempWall.getX()
  60. && (y + 20) >= tempWall.getY()) {
  61. return tempWall.canBeWalk();
  62. }
  63. }
  64. }
  65. }
  66. for (int i = 0; i Tank tempTank = tanks.get(i);
  67. if (tempTank == this)
  68. break;
  69. if (tempTank.isAlive()) {
  70. if (x >= tempTank.getX() && y >= tempTank.getY()) {
  71. if (x return false;
  72. }
  73. } else if (x >= tempTank.getX() && y if (x && (y + 20) >= tempTank.getY()) {
  74. return false;
  75. }
  76. } else if (x = tempTank.getY()) {
  77. if ((x + 20) >= tempTank.getX()
  78. && y return false;
  79. }
  80. } else if (x if ((x + 20) >= tempTank.getX()
  81. && (y + 20) >= tempTank.getY()) {
  82. return false;
  83. }
  84. }
  85. }
  86. }
  87. return true;
  88. }
  89. public void damage(int power) {
  90. lifes -= power;
  91. if (lifes alive = false;
  92. }
  93. }
  94. public ArrayList getBullet() {
  95. return bullets;
  96. }
  97. public Color getColor() {
  98. return color;
  99. }
  100. public int getDirection() {
  101. return direction;
  102. }
  103. public int getImageID() {
  104. return tankImageID;
  105. }
  106. public int getSpeed() {
  107. return speed;
  108. }
  109. public ArrayList getWalls() {
  110. return this.walls;
  111. }
  112. public int getX() {
  113. return x;
  114. }
  115. public int getY() {
  116. return y;
  117. }
  118. public boolean isAlive() {
  119. return alive;
  120. }
  121. public void move(int direction) {
  122. setDirection(direction);
  123. int x = this.x;
  124. int y = this.y;
  125. switch (direction) {
  126. case UP:
  127. y -= speed;
  128. break;
  129. case DOWN:
  130. y += speed;
  131. break;
  132. case RIGHT:
  133. x += speed;
  134. break;
  135. case LEFT:
  136. x -= speed;
  137. break;
  138. }
  139. if (canMove(x, y)) {
  140. this.x = x;
  141. this.y = y;
  142. }
  143. }
  144. public void setAllTanks(ArrayList tanks) {
  145. this.tanks = tanks;
  146. }
  147. public void setDirection(int direction) {
  148. this.direction = direction;
  149. }
  150. final protected void setImageID(int ID) {
  151. this.tankImageID = ID;
  152. }
  153. public void setSpeed(int speed) {
  154. this.speed = speed;
  155. }
  156. public void setWalls(ArrayList wallList) {
  157. this.walls = wallList;
  158. }
  159. public void shot() {
  160. switch (direction) {
  161. case UP:
  162. bullet = new NormalBullet(x + 10, y, UP);
  163. break;
  164. case DOWN:
  165. bullet = new NormalBullet(x + 10, y + 20, DOWN);
  166. break;
  167. case RIGHT:
  168. bullet = new NormalBullet(x + 20, y + 10, RIGHT);
  169. break;
  170. case LEFT:
  171. bullet = new NormalBullet(x, y + 10, LEFT);
  172. break;
  173. }
  174. for (int i = 0; i Bullet temp = bullets.get(i);
  175. if (!temp.isAlive()) {
  176. bullets.remove(temp);
  177. }
  178. }
  179. if (bullets.size() >= maxBulletNum) {
  180. } else {
  181. new Thread(bullet).start();
  182. bullets.add(bullet);
  183. }
  184. }
  185. }
复制代码
  1. package tank.common;
  2. public abstract class Wall {
  3. private int x, y;
  4. private int wallImageID;
  5. private boolean canWalk;
  6. private boolean canFly;
  7. private boolean alive;
  8. private boolean canHit;
  9. public Wall(int x, int y, int ID, boolean walk, boolean fly, boolean Hit) {
  10. this.x = x;
  11. this.y = y;
  12. this.wallImageID = ID;
  13. this.canWalk = walk;
  14. this.canFly = fly;
  15. this.alive = true;
  16. this.canHit = Hit;
  17. }
  18. public boolean canBeFly() {
  19. return canFly;
  20. }
  21. public boolean canBeHit() {
  22. return this.canHit;
  23. }
  24. public boolean canBeWalk() {
  25. return canWalk;
  26. }
  27. public void die() {
  28. alive = false;
  29. }
  30. public int getImageID() {
  31. return wallImageID;
  32. }
  33. public int getX() {
  34. return x;
  35. }
  36. public int getY() {
  37. return y;
  38. }
  39. public boolean isAlive() {
  40. return alive;
  41. }
  42. }
复制代码
  1. package tank.entity;
  2. public class Bomb {
  3. private int x;
  4. private int y;
  5. private int life = 9;
  6. private boolean alive;
  7. public Bomb(int x, int y) {
  8. this.x = x;
  9. this.y = y;
  10. alive = true;
  11. }
  12. public void decrese() {
  13. if (life > 0) {
  14. life--;
  15. } else {
  16. alive = false;
  17. }
  18. }
  19. public int getLife() {
  20. return life;
  21. }
  22. public int getX() {
  23. return x;
  24. }
  25. public int getY() {
  26. return y;
  27. }
  28. public boolean isAlive() {
  29. return alive;
  30. }
  31. }
复制代码
  1. package tank.entity;
  2. import tank.common.EnemyTank;
  3. import tank.common.Tank;
  4. public class EnemyTank1 extends EnemyTank {
  5. public EnemyTank1(int x, int y, Tank hero) {
  6. super(x, y, hero, 3, 1);
  7. setSpeed(2);
  8. setShotSpeed(0.8);
  9. }
  10. }
复制代码
  1. package tank.entity;
  2. import tank.common.EnemyTank;
  3. import tank.common.Tank;
  4. public class EnemyTank2 extends EnemyTank {
  5. public EnemyTank2(int x, int y, Tank hero) {
  6. super(x, y, hero, 5, 2);
  7. setSpeed(3);
  8. setShotSpeed(0.5);
  9. }
  10. }
复制代码
  1. package tank.entity;
  2. import tank.common.EnemyTank;
  3. import tank.common.Tank;
  4. public class EnemyTank3 extends EnemyTank {
  5. public EnemyTank3(int x, int y, Tank hero) {
  6. super(x, y, hero, 10, 3);
  7. setSpeed(5);
  8. setShotSpeed(0.2);
  9. }
  10. }
复制代码
  1. package tank.entity;
  2. import tank.common.Wall;
  3. public class Grass extends Wall {
  4. public Grass(int x, int y) {
  5. super(x, y, 3, true, true, false);
  6. }
  7. }
复制代码
  1. package tank.entity;
  2. import java.awt.Color;
  3. import tank.common.Tank;
  4. public class MyTank extends Tank {
  5. public MyTank(int x, int y) {
  6. super(x, y, Color.yellow);
  7. lifes = 5;
  8. super.setImageID(0);
  9. }
  10. public int getLife() {
  11. return lifes;
  12. }
  13. }
复制代码
  1. package tank.entity;
  2. import tank.common.Bullet;
  3. public class NormalBullet extends Bullet {
  4. public NormalBullet(int x, int y, int direction) {
  5. super(x, y, 2, direction);
  6. power = 1;
  7. }
  8. }
复制代码
  1. package tank.entity;
  2. import java.util.ArrayList;
  3. import tank.common.EnemyTank;
  4. public class Stage {
  5. private int totalEnemyNum;
  6. private int leaveEnemyNum;
  7. private int totalHeroLife;
  8. private int leaveHeroLife;
  9. private int level;
  10. private WallContainer wallContainer;
  11. private ArrayList enemeyTanks;
  12. private int activeEnemyTankNum;
  13. private EnemyTank activeEnemyTanks[];
  14. private MyTank hero;
  15. public Stage(int totalEnemyNum, int totalHeroLife, int level,
  16. int activeEnemyTankNum) {
  17. this.totalEnemyNum = totalEnemyNum;
  18. this.totalHeroLife = totalHeroLife;
  19. this.level = level;
  20. this.activeEnemyTankNum = activeEnemyTankNum;
  21. this.leaveEnemyNum = this.totalEnemyNum;
  22. this.leaveHeroLife = this.totalHeroLife;
  23. this.enemeyTanks = new ArrayList();
  24. this.activeEnemyTanks = new EnemyTank[this.activeEnemyTankNum];
  25. this.hero = new MyTank(290, 370);
  26. this.wallContainer = new WallContainer();
  27. }
  28. public void addEnemyTank(EnemyTank tank) {
  29. enemeyTanks.add(tank);
  30. }
  31. public void autoCreateEnemyTank() {
  32. for (int i = 0; i double key = Math.random();
  33. if (key this.enemeyTanks.add(new EnemyTank1(0, 0, hero));
  34. } else if (key >= 0.66) {
  35. this.enemeyTanks.add(new EnemyTank2(0, 0, hero));
  36. } else {
  37. this.enemeyTanks.add(new EnemyTank3(0, 0, hero));
  38. }
  39. }
  40. }
  41. public int getActiveEnemyTankNum() {
  42. return this.activeEnemyTankNum;
  43. }
  44. public EnemyTank[] getEnemyTank() {
  45. return this.activeEnemyTanks;
  46. }
  47. public MyTank getHeroTank() {
  48. return this.hero;
  49. }
  50. public int getLeaveEnemyNum() {
  51. return this.leaveEnemyNum;
  52. }
  53. public int getLeaveHeroLife() {
  54. return this.leaveHeroLife;
  55. }
  56. public int getLevel() {
  57. return this.level;
  58. }
  59. public WallContainer getWallContainer() {
  60. return this.wallContainer;
  61. }
  62. public boolean isHeroDead() {
  63. if (this.leaveHeroLife return true;
  64. } else
  65. return false;
  66. }
  67. public EnemyTank popEnemyTank() {
  68. if (leaveEnemyNum > 0) {
  69. this.leaveEnemyNum--;
  70. EnemyTank temp = enemeyTanks.get(enemeyTanks.size() - 1);
  71. enemeyTanks.remove(temp);
  72. return temp;
  73. } else
  74. return null;
  75. }
  76. public MyTank popHero() {
  77. leaveHeroLife--;
  78. MyTank temp = new MyTank(290, 370);
  79. temp.setWalls(wallContainer.getWallList());
  80. return temp;
  81. }
  82. public void setHeroTank(MyTank tank) {
  83. this.hero = tank;
  84. }
  85. }
复制代码
  1. package tank.entity;
  2. import tank.common.Wall;
  3. public class Steels extends Wall {
  4. public Steels(int x, int y) {
  5. super(x, y, 2, false, false, false);
  6. }
  7. }
复制代码
  1. package tank.entity;
  2. import java.util.ArrayList;
  3. import tank.common.Common;
  4. import tank.common.Wall;
  5. public class WallContainer implements Common {
  6. private ArrayList data;
  7. public WallContainer() {
  8. data = new ArrayList();
  9. for (int i = 0; i this.addWall(10 + i * 20, 100, WATER);
  10. if (i == 11) {
  11. i += 4;
  12. }
  13. }
  14. for (int i = 0; i this.addWall(10 + i * 20, 160, WALLS);
  15. if (i == 12) {
  16. i += 4;
  17. }
  18. }
  19. for (int i = 0; i this.addWall(10 + i * 20, 220, STEELS);
  20. if (i == 11) {
  21. i += 4;
  22. }
  23. }
  24. for (int i = 0; i this.addWall(10 + i * 20, 280, GRASS);
  25. if (i == 12) {
  26. i += 4;
  27. }
  28. }
  29. }
  30. public void addWall(int x, int y, int kind) {
  31. switch (kind) {
  32. case WATER:
  33. data.add(new Water(x, y));
  34. break;
  35. case WALLS:
  36. data.add(new Walls(x, y));
  37. break;
  38. case STEELS:
  39. data.add(new Steels(x, y));
  40. break;
  41. case GRASS:
  42. data.add(new Grass(x, y));
  43. break;
  44. }
  45. }
  46. public ArrayList getWallList() {
  47. ArrayList temp = data;
  48. return temp;
  49. }
  50. public boolean isEmpty() {
  51. return data.isEmpty();
  52. }
  53. public void removeDead() {
  54. for (int i = 0; i Wall temp = data.get(i);
  55. if (!temp.isAlive())
  56. data.remove(temp);
  57. }
  58. }
  59. }
复制代码
  1. package tank.entity;
  2. import tank.common.Wall;
  3. public class Walls extends Wall {
  4. public Walls(int x, int y) {
  5. super(x, y, 1, false, false, true);
  6. }
  7. }
复制代码
  1. package tank.entity;
  2. import tank.common.Wall;
  3. public class Water extends Wall {
  4. public Water(int x, int y) {
  5. super(x, y, 0, false, true, false);
  6. }
  7. }
复制代码
  1. package tank.gui;
  2. import javax.swing.JFrame;
  3. import javax.swing.JMenu;
  4. import javax.swing.JMenuBar;
  5. import javax.swing.JMenuItem;
  6. public class TankFrame extends JFrame {
  7. /**
  8. *
  9. */
  10. private static final long serialVersionUID = 1L;
  11. private TankPanel gamePanel;
  12. public TankFrame() {
  13. super("坦克大战————玄雨制作");
  14. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. // 添加游戏主面板
  16. gamePanel = new TankPanel();
  17. this.add(gamePanel);
  18. gamePanel.addMouseListener(gamePanel);
  19. this.addKeyListener(gamePanel);
  20. // 添加菜单栏
  21. JMenuBar menuBar = new JMenuBar();
  22. // ///////////////////////////////
  23. JMenu menu1 = new JMenu("菜单");
  24. menuBar.add(menu1);
  25. JMenuItem itemNewGame = new JMenuItem("新游戏");
  26. menu1.add(itemNewGame);
  27. menu1.addSeparator();
  28. JMenuItem itemList = new JMenuItem("排行榜");
  29. menu1.add(itemList);
  30. menu1.addSeparator();
  31. JMenuItem itemExit = new JMenuItem("退出");
  32. menu1.add(itemExit);
  33. // //////////////////////////////////
  34. JMenu menu2 = new JMenu("设置");
  35. JMenuItem itemSet = new JMenuItem("设置");
  36. menu2.add(itemSet);
  37. menuBar.add(menu2);
  38. // /////////////////////////////////
  39. JMenu menu3 = new JMenu("帮助");
  40. menuBar.add(menu3);
  41. JMenuItem itemInfo = new JMenuItem("关于");
  42. menu3.add(itemInfo);
  43. JMenuItem itemHelp = new JMenuItem("帮助");
  44. menu3.add(itemHelp);
  45. this.setJMenuBar(menuBar);
  46. this.setResizable(false);
  47. this.pack();
  48. this.setVisible(true);
  49. }
  50. }
复制代码
  1. package tank.start;
  2. import tank.gui.TankFrame;
  3. public class TankStart {
  4. /**
  5. * @param args
  6. */
  7. public static void main(String[] args) {
  8. // TODO Auto-generated method stub
  9. new TankFrame().setLocation(250, 150);
  10. }
  11. }
复制代码


Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

Video Face Swap

Video Face Swap

Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Wie funktioniert die Session -Entführung und wie können Sie es in PHP mildern? Wie funktioniert die Session -Entführung und wie können Sie es in PHP mildern? Apr 06, 2025 am 12:02 AM

Die Hijacking der Sitzung kann in den folgenden Schritten erreicht werden: 1. Erhalten Sie die Sitzungs -ID, 2. Verwenden Sie die Sitzungs -ID, 3. Halten Sie die Sitzung aktiv. Zu den Methoden zur Verhinderung der Sitzung der Sitzung in PHP gehören: 1. Verwenden Sie die Funktion Session_regenerate_id (), um die Sitzungs -ID zu regenerieren. 2. Store -Sitzungsdaten über die Datenbank, 3. Stellen Sie sicher, dass alle Sitzungsdaten über HTTPS übertragen werden.

Erklären Sie JSON Web Tokens (JWT) und ihren Anwendungsfall in PHP -APIs. Erklären Sie JSON Web Tokens (JWT) und ihren Anwendungsfall in PHP -APIs. Apr 05, 2025 am 12:04 AM

JWT ist ein offener Standard, der auf JSON basiert und zur sicheren Übertragung von Informationen zwischen Parteien verwendet wird, hauptsächlich für die Identitätsauthentifizierung und den Informationsaustausch. 1. JWT besteht aus drei Teilen: Header, Nutzlast und Signatur. 2. Das Arbeitsprinzip von JWT enthält drei Schritte: Generierung von JWT, Überprüfung von JWT und Parsingnayload. 3. Bei Verwendung von JWT zur Authentifizierung in PHP kann JWT generiert und überprüft werden, und die Funktionen und Berechtigungsinformationen der Benutzer können in die erweiterte Verwendung aufgenommen werden. 4. Häufige Fehler sind Signaturüberprüfungsfehler, Token -Ablauf und übergroße Nutzlast. Zu Debugging -Fähigkeiten gehört die Verwendung von Debugging -Tools und Protokollierung. 5. Leistungsoptimierung und Best Practices umfassen die Verwendung geeigneter Signaturalgorithmen, das Einstellen von Gültigkeitsperioden angemessen.

Beschreiben Sie die soliden Prinzipien und wie sie sich für die PHP -Entwicklung anwenden. Beschreiben Sie die soliden Prinzipien und wie sie sich für die PHP -Entwicklung anwenden. Apr 03, 2025 am 12:04 AM

Die Anwendung des soliden Prinzips in der PHP -Entwicklung umfasst: 1. Prinzip der Einzelverantwortung (SRP): Jede Klasse ist nur für eine Funktion verantwortlich. 2. Open and Close Principle (OCP): Änderungen werden eher durch Erweiterung als durch Modifikation erreicht. 3.. Lischs Substitutionsprinzip (LSP): Unterklassen können Basisklassen ersetzen, ohne die Programmgenauigkeit zu beeinträchtigen. 4. Schnittstellen-Isolationsprinzip (ISP): Verwenden Sie feinkörnige Schnittstellen, um Abhängigkeiten und nicht verwendete Methoden zu vermeiden. 5. Abhängigkeitsinversionsprinzip (DIP): Hoch- und niedrige Module beruhen auf der Abstraktion und werden durch Abhängigkeitsinjektion implementiert.

Wie debugge ich den CLI -Modus in PhpStorm? Wie debugge ich den CLI -Modus in PhpStorm? Apr 01, 2025 pm 02:57 PM

Wie debugge ich den CLI -Modus in PhpStorm? Bei der Entwicklung mit PHPSTORM müssen wir manchmal den PHP im CLI -Modus (COMS -Zeilenschnittstellen) debuggen ...

Wie setze ich nach dem Neustart des Systems automatisch Berechtigungen von Unixsocket fest? Wie setze ich nach dem Neustart des Systems automatisch Berechtigungen von Unixsocket fest? Mar 31, 2025 pm 11:54 PM

So setzen Sie die Berechtigungen von Unixsocket automatisch nach dem Neustart des Systems. Jedes Mal, wenn das System neu startet, müssen wir den folgenden Befehl ausführen, um die Berechtigungen von Unixsocket: sudo ...

Erklären Sie die späte statische Bindung in PHP (statisch: :). Erklären Sie die späte statische Bindung in PHP (statisch: :). Apr 03, 2025 am 12:04 AM

Statische Bindung (statisch: :) implementiert die späte statische Bindung (LSB) in PHP, sodass das Aufrufen von Klassen in statischen Kontexten anstatt Klassen zu definieren. 1) Der Analyseprozess wird zur Laufzeit durchgeführt.

Wie sende ich eine Postanforderung mit JSON -Daten mithilfe der Curl -Bibliothek von PHP? Wie sende ich eine Postanforderung mit JSON -Daten mithilfe der Curl -Bibliothek von PHP? Apr 01, 2025 pm 03:12 PM

Senden von JSON -Daten mithilfe der Curl -Bibliothek von PHP in der PHP -Entwicklung müssen häufig mit externen APIs interagieren. Eine der gängigen Möglichkeiten besteht darin, die Curl Library zu verwenden, um Post � ...

See all articles