
正文
打台球代码java,c语言台球游戏代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
用java继承,多态与接口
题目有关回答如下,供参考:
4、this:当前对象的引用
super:当前对象的超(父)类对象的一个引用
5、继承是面向对象最显著的一个特性。
继承的意义:继承是从已有的类中派生出新的类,新的类能吸收已有类的数据属性和行为,并能扩展新的能力。
定义继承:使用关键字 extends 来实现继承
6、多态(Polymorphism)按字面的意思就是“多种状态”,也是面向对象的一个特性。
允许将子类类型的引用赋值给父类类型的引用。
7、代码如下:
/**
* 父类(图形)
*
* @author qd
*
*/
abstract class Figure {
double area;
}
/**
* 求面积的接口
*
* @author qd
*
*/
interface Area {
public void getArea();
}
/**
* 三角形
*
* @author qd
*
*/
class Triangle extends Figure implements Area {
// 底
double bottom;
// 高
double high;
public Triangle(double bottom, double high) {
super();
this.bottom = bottom;
this.high = high;
}
@Override
public void getArea() {
area = bottom * high * 0.5;
System.out.println("三角形面积是:" + area);
}
}
/**
* 正方形
*
* @author qd
*
*/
class Square extends Figure implements Area {
// 边长
double length;
public Square(double length) {
super();
this.length = length;
}
@Override
public void getArea() {
area = length * length;
System.out.println("正方形面积是:" + area);
}
}
/**
* 圆
*
* @author qd
*
*/
class Circular extends Figure implements Area {
// 半径
double radius;
public Circular(double radius) {
super();
this.radius = radius;
}
@Override
public void getArea() {
area = Math.PI * radius * radius;
System.out.println("圆面积是:" + area);
}
}
public class Test {
public static void main(String[] args) {
// 三角形对象
Area triangle = new Triangle(3, 4);
triangle.getArea();
// 正方形对象
Area square = new Square(4);
square.getArea();
// 圆对象
Area circular = new Circular(2);
circular.getArea();
}
}
8、代码如下:
/**
* 球类
*
* @author qd
*
*/
class Ball {
// 私有成员变量半径
private double r;
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
}
/**
* 台球
*
* @author qd
*
*/
class Billiards extends Ball {
// 私有成员变量颜色
private String color;
public void setColor(String color) {
this.color = color;
}
// 输出信息
public void printMess(Billiards billiards,Ball ball) {
System.out.println("台球的颜色是:" + billiards.color + " 台球的半径是:" + ball.getR());
}
}
/**
* 公有测试类
*
* @author qd
*
*/
public class TestBall {
public static void main(String[] args) {
Ball ball = new Ball();
ball.setR(5);
Billiards billiards = new Billiards();
billiards.setColor("白色");
billiards.printMess(billiards,ball);
}
}
7题和8题测试如下:
相关问答
Q1: 关于台球游戏的C语言编程
#includestdio.h
#includegraphics.h
#includeconio.h
#include stdlib.h
#includemath.h
#includetime.h
#define PATH "D:\\tc30"
#define pi 3.14159265354
#define r 10.0
#define zero 0.01
#define BC 0
#define DISK 2
float R=r+0.5;
struct balltype
{
float x,y;
float dx,dy;
int flag;
}ball[16];
int cx=320-5*r,cy=240;
float power=15.0;
int flag=1,player=0,chang=0;
int hole[6][2]={{17,92},{320,92},{623,92},
{17,388},{320,388},{623,388}};
int plball[2]={0,0},ex=1;
void help()
{ char *fname = { "a/4:left d/6:right w/8:up s/2:down Space:shoot ,or.:power Esc:exit" };
setcolor(14);
outtextxy(8,460,fname);
}
void waittime(double t)
{
time_t t1,t2;
double a,b;
a=time(t1);
while(1)
{
b=time(t2);
if(b-at) break;
}
}
void drawhlep()
{
setbkcolor(9);
setcolor(14);
rectangle(50,50,550,400);
rectangle(50,100,550,145);
settextstyle(0, 0, 4);
outtextxy(150, 60, "Help You!");
rectangle(250,100,400,145);
setcolor(4);
settextstyle(3,0,4);
outtextxy(270,102,"Player1");
outtextxy(420,102,"Player2");
setcolor(14);
rectangle(50,170,550,195);
rectangle(50,220,550,245);
rectangle(50,270,550,295);
rectangle(50,320,550,345);
rectangle(50,370,550,400);
rectangle(400,145,550,245);
rectangle(50,145,250,370);
setcolor(10);
settextstyle(3,0,1);
outtextxy(115,146,"Left");
outtextxy(115,171,"Right");
outtextxy(115,196,"Up");
outtextxy(115,221,"Down");
outtextxy(115,246,"Move Quick");
outtextxy(115,271,"Add");
outtextxy(115,296,"Subtrate");
outtextxy(115,321,"Shoot");
outtextxy(115,346,"Exit");
setcolor(5);
settextstyle(7,0,4);
outtextxy(60,385,"Welcome to ZGQ's Game. QQ:271111716 E-mail:zgq150@163.com");
}
Q2: 写一个最简单的JAVA继承代码??谢谢
可运行的:
import java.awt.*;
import java.awt.event.*;
public class BackJFrame extends Frame{
public BackJFrame(){
super("台球");
setSize(300,300);
setBackground(Color.cyan); //背景
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing (WindowEvent e)
{System.exit(0);}
} );
}
public static void main(String args[]){
new BackJFrame();
}
}
Q3: 最近在看高淇老师的java300集,第22集的时候是写一个桌球小游戏,但是里面的图片加载不出来。求大神解答
图片路径不对,你这里用的是想对路径,图片资源要放在项目加载资源部分才行,不然就改成绝对路径
Q4: 我用JAVAweb写了一个台球收费系统,比如说2号桌,有人在玩了 ,那么别人就不能在2号桌关灯之前在开灯了,
给每桌的灯加个属性状态,当开灯时把状态改变,然后每次想要开灯时判断该状态,如果已经开了,就不允许开了,否则才可以开灯!






