一寫就寫多了,不過包含的東西比較多,java的重要知識點里面也有
創(chuàng)新互聯(lián)自2013年起,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元東方做網(wǎng)站,已為上家服務(wù),為東方各地企業(yè)和個人服務(wù),聯(lián)系電話:13518219792
這個結(jié)構(gòu)是簡單的處理的,有興趣的話你可以想象下寫成一個小的文字游戲
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() {
//你可以嘗試在這里做一個狀態(tài)機,控制動物的行為
int i = 0 ;
int state = 0 ;
while(i life)
{
double rnd = Math.random();
state = (int)(rnd * 4);//隨機出狀態(tài)
active(state);
i++;
}
System.out.println("小貓" + name + "它的生命結(jié)束了,享年" + 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() {
//你可以嘗試在這里做一個狀態(tài)機,控制動物的行為
int i = 0 ;
int state = 0 ;
while(i life)
{
double rnd = Math.random();
state = (int)(rnd * 4);//隨機出狀態(tài)
active(state);
i++;
}
System.out.println("小狗" + name + "它的生命結(jié)束了,享年" + 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類型,不能直接轉(zhuǎn)化為int,因此要改寫為
int s;
s = Integer.valueOf(in.readLine());
并在函數(shù)聲明出加上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";
}
經(jīng)測無誤
名稱欄目:JAVA狗叫源代碼 java源程序是什么意思
轉(zhuǎn)載來于:http://m.rwnh.cn/article46/doopihg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、微信小程序、建站公司、營銷型網(wǎng)站建設(shè)、搜索引擎優(yōu)化、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)