十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
一写就写多了,不过包含的东西比较多,java的重要知识点里面也有
创新互联自2013年起,是专业互联网技术服务公司,拥有项目成都网站建设、成都网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元东方做网站,已为上家服务,为东方各地企业和个人服务,联系电话:13518219792
这个结构是简单的处理的,有兴趣的话你可以想象下写成一个小的文字游戏
public class D {
public static void main(String[] args){
Cat cat1 = new Cat("Tom" , 10);
Cat cat2 = new Cat("Sun" , 15 , "苹果树");
Dog dog1 = new Dog("lufy" , 5);
Dog dog2 = new Dog("nan" , 20 , "泳池");
cat1.start();
cat2.start();
dog1.start();
dog2.start();
}
}
abstract class Creature extends Thread
{
protected int life; //生物的寿命
protected String name = "";
protected String objname = "";
public void call(){};
public void eat(){};
public void active(int state){};
}
class Cat extends Creature
{
public Cat(String name , int life)
{
this.name = name;
this.life = life;
System.out.println("小猫" + name + "诞生了 , 寿命" + life);
}
public Cat(String name , int life ,String objname)
{
this.name = name;
this.life = life;
this.objname = objname;
System.out.println("小猫" + name + "诞生了 , 寿命" + life + "喜欢" + objname );
}
public void climb()
{
System.out.println("小猫" + name + "在爬树" );
}
public void climb(String objname)
{
System.out.println("小猫" + name + "在爬" + objname + "树");
}
@Override
public void call() {
System.out.println("小猫" + name + "停下来在喵喵的叫" );
}
@Override
public void eat() {
System.out.println("小猫" + name + "饿了,它在吃东西" );
}
@Override
public void active(int state) {
switch(state)
{
case 1:
if(objname.length() 0)
climb(objname);
else
climb();
break;
case 2:
call();
break;
case 3:
eat();
break;
}
}
@Override
public void run() {
//你可以尝试在这里做一个状态机,控制动物的行为
int i = 0 ;
int state = 0 ;
while(i life)
{
double rnd = Math.random();
state = (int)(rnd * 4);//随机出状态
active(state);
i++;
}
System.out.println("小猫" + name + "它的生命结束了,享年" + life);
}
}
class Dog extends Creature
{
public Dog(String name , int life)
{
this.name = name;
this.life = life;
System.out.println("小狗" + name + "诞生了 , 寿命" + life );
}
public Dog(String name , int life , String objname)
{
this.name = name;
this.life = life;
this.objname = objname;
System.out.println("小狗" + name + "诞生了 , 寿命" + life + "喜欢" + objname );
}
public void swim()
{
System.out.println("小狗" + name + "在游泳" );
}
public void swim(String objname)
{
System.out.println("小狗" + name + "在" + objname + "游泳");
}
@Override
public void call() {
System.out.println("小狗" + name + "停下来在喵喵的叫" );
}
@Override
public void eat() {
System.out.println("小狗" + name + "饿了,它在吃东西" );
}
@Override
public void active(int state) {
switch(state)
{
case 1:
if(objname.length() 0)
swim(objname);
else
swim();
break;
case 2:
call();
break;
case 3:
eat();
break;
}
}
@Override
public void run() {
//你可以尝试在这里做一个状态机,控制动物的行为
int i = 0 ;
int state = 0 ;
while(i life)
{
double rnd = Math.random();
state = (int)(rnd * 4);//随机出状态
active(state);
i++;
}
System.out.println("小狗" + name + "它的生命结束了,享年" + life);
}
}
interface animal {
void shout();
}
class Dog implements animal{
public void shout(){
System.out.println("汪汪叫");
}
}
class Cat implements animal{
public void shout(){
System.out.println("喵喵叫");
}
}
class Bird implements animal{
public void shout(){
System.out.println("唧唧叫");
}
}
package com.java.basic;
public class AbstractTest {
public static void main(String[] args) {
Dog dog = new Dog();
dog.call();
Cat cat = new Cat();
cat.call();
}
}
/**
* 宠物类
*
* @author Administrator
*
*/
abstract class Pet {
/*
* 叫
*/
abstract void call();
}
/**
* 犬类
*
* @author Administrator
*
*/
class Dog extends Pet {
/*
* 犬类有自己独特的叫声
*/
void call() {
System.out.println("汪汪汪!");
}
}
/**
* 猫类
*
* @author Administrator
*
*/
class Cat extends Pet {
/*
* 猫类有自己独特的叫声
*/
void call() {
System.out.println("喵喵喵!");
}
}
错在:
int s;
s=in.readLine();
in.readLine()得到的是String类型,不能直接转化为int,因此要改写为
int s;
s = Integer.valueOf(in.readLine());
并在函数声明出加上throws语句如下:
public static void main(String[] args) throws NumberFormatException, IOException
修改后代码为.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class jiaojiao implements dog, cat, niu {
public static void main(String[] args) throws NumberFormatException, IOException {
System.out.println("输入序号以让各个动物叫,1是狗,2是猫,3是牛");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int s;
s = Integer.valueOf(in.readLine());
switch (s) {
case 1:
System.out.print("动物名字" + dog.name + "动物叫声" + dog.jiao);
;
break;
case 2:
System.out.print("动物名字" + cat.name + "动物叫声" + cat.jiao);
;
break;
case 3:
System.out.print("动物名字" + niu.name + "动物叫声" + niu.jiao);
break;
}
}
}
interface animal {
String jiao = " ";
String name = " ";
}
// 狗接口继承动物的接口
interface dog extends animal {
String jiao = "wangwang";
String name = "dogs";
}
// 猫接口继承动物的接口
interface cat extends animal {
String jiao = "miaomiao";
String name = "cats";
}
// 牛接口继承动作的接口
interface niu extends animal {
String jiao = "niuniu";
String name = "nius";
}
经测无误