java喂狗
❶ java定義一個動物類 Dog 有屬性 行為 == 求詳細步驟 謝謝 很急很急 在線等。
package com.java;
/聲明一個抽象類動物
abstract class Animal {
//聲明一個方法gnaw()啃骨頭
public void gnaw() {
System.out.println("Animal gnaw(啃骨頭)..");
}
//喝湯
public void eat() {
System.out.println("Animal eat(喝湯)...");
}
//咬人
public void bite() {
System.out.println("Animal bite(咬人)...");
}
//跑
public void run() {
System.out.println("Animal run(咬人)...");
}
//跳
public void jump() {
System.out.println("Animal jump...");
}
}
//聲明一個Dog類,繼承Animal類
class Dog extends Animal{
private String name;
private double weight;
private double height;
private String color;
public Dog (String name,String color){
this.name=name;
this.color=color;
}
}
//以下是測試結果,可以省略
public class NLL {
public static void main(String[] args) {
Animal smallDog =new Dog("smallDog","yellow");
Animal bigDog = new Dog("bigDog ","yellow");
smallDog.gnaw();
smallDog.eat();
smallDog.bite();
bigDog .bite();
}
}
❷ Java 狗狗類
publicclassDog{
/**
*昵稱
*/
privateStringnickname;
/**
*品種
*/
privateStringtype;
/**
*顏色
*/
privateStringcolor;
publicvoidselfIntroction(){
System.out.println("Dog{"+
"昵稱='"+nickname+'''+
",品種='"+type+'''+
",顏色='"+color+'''+
'}');
}
publicIntegerspeed(){
//不清楚具體需求可額外設置個屬性返回該屬性或在該方法中寫自己的計算公式
return1;
}
publicDog(){
}
publicDog(Stringnickname,Stringtype,Stringcolor){
this.nickname=nickname;
this.type=type;
this.color=color;
}
publicStringgetNickname(){
returnnickname;
}
publicvoidsetNickname(Stringnickname){
this.nickname=nickname;
}
publicStringgetType(){
returntype;
}
publicvoidsetType(Stringtype){
this.type=type;
}
publicStringgetColor(){
returncolor;
}
publicvoidsetColor(Stringcolor){
this.color=color;
}
}
❸ java建立貓cat類和狗類,具備叫聲的方法cry(),
/*定義動物父類*/
classAnimal{
voidcry(){
System.out.println("Animalcry");
}
}
classCatextendsAnimal{
voidcry(){
System.out.println("catcry");
}
}
classDogextendsAnimal{
voidcry(){
System.out.println("dogcry");
}
}
publicclassExe2{
publicstaticvoidcry(Animalanimal){
animal.cry();
}
publicstaticvoidmain(String[]args){
Animalanimal=newAnimal();
cry(animal);
Catcat=newCat();
cry(cat);
Dogdog=newDog();
cry(dog);
}
}
❹ JAVA代碼主人喂寵物吃東西狗只吃骨頭貓只吃魚求代碼用多態
/*
animal是個抽象方法,Cat和Dogextends這個就是用的多態
*/
packageTest;
publicclassTest{
publicstaticvoidmain(String[]args){
Feederfeeder=newFeeder();
feeder.feedAnimals();
}
}
abstractclassAnimal{
publicabstractvoideat(Strings);
}
classDogextendsAnimal{
privatefinalStringFOOD="bone";
@Override
publicvoideat(Strings){
if(s==FOOD)
System.out.println("Dogiseatingbones");
else
System.out.println("Not"+this.FOOD+",Dogdon'twanttoeat");
}
}
classCatextendsAnimal{
privatefinalStringFOOD="fish";
@Override
publicvoideat(Strings){
if(s==FOOD)
System.out.println("Catiseatingfishes");
else
System.out.println("Not"+this.FOOD+",Catdon'twanttoeat");
}
}
classFeeder{
privatefinalString[]FOODS={"fish","bone","shit"};
privateAnimalcat;
privateAnimaldog;
Feeder(){
dog=newDog();
cat=newCat();
}
publicvoidfeedAnimals(){
System.out.println("Feedinganimals...");
Stringfood;
for(inti=0;i<FOODS.length;i++){
food=FOODS[i];
if(food=="fish")
this.cat.eat(food);
elseif(food=="bone")
this.dog.eat(food);
else{
System.out.println("NotFishesorBones,is"+food);
}
}
System.out.println("Done!");
}
}
❺ java 線程 喚醒問題
首先,從你程序的本意來看, 你是想用線程實現一個生產者-- 消費者模式, 用在饅頭的場景, 如下:
1、 肯定需要一個籃子, 負責裝饅頭,並且這個籃子有容量,假設容量為C;
2、 有兩個人,第一個是爹(生產者), 第二個是娃(消費者), 爹負責向籃子中放入饅頭, 娃負責從籃子中取出饅頭喂狗;
3、 但是籃子的容量是有限的, 當籃子被裝滿時, 爹就等待娃從籃子中取出饅頭,籃子騰出空間之後,爹繼續裝饅頭; 當籃子的饅頭都被取走後, 娃就等待, 當籃子中又被放入饅頭時,才繼續取饅頭。
附上代碼:
籃子:
public class Bucket {
private int count = 0; // 籃子能夠容納的饅頭數
private int total = 0; // 放饅頭的總數
public synchronized void put(){
if(count == 5){
try {
System.out.println("俺是他爹,籃子滿了,俺在等俺家娃拿饅頭喂狗");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count += 1;
total++;
System.out.println("俺是他爹,俺放了一個饅頭,現有籃子里有 [" + count + "] 個饅頭");
notify();
}
public synchronized boolean get(){
if(count == 0){
try {
wait();
System.out.println("俺是他娃,籃子空了,俺在等俺爹放饅頭到籃子里");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count -= 1;
System.out.println("俺拿了一個饅頭喂俺家大花狗, 籃子里還有 [" + count + "] 個饅頭");
notify();
if(total == 100 && count == 0){
return false;
}else{
return true;
}
}
爹:
public class Proct implements Runnable{
private Bucket bucket;
Proct(Bucket bucket){
this.bucket = bucket;
}
@Override
public void run() {
for(int i=0; i<100; i++){
bucket.put();
}
System.out.println("俺把饅頭都放完了");
}
}
娃:
public class Consumer implements Runnable{
private Bucket bucket;
public Consumer(Bucket bucket){
this.bucket = bucket;
}
@Override
public void run() {
while(true){
if(!bucket.get()){
break;
}
}
System.out.println("俺家大花狗吃完饅頭了");
}
}
啟動類:
public class Test {
public static void main(String[] args) {
Bucket bucket = new Bucket();
new Thread(new Consumer(bucket)).start();
new Thread(new Proct(bucket)).start();
}
}
最後附上你的問題, notify 由誰來喚醒:
注意 wait() 和 notify() 是Object類上的方法,
notify() 的意思是: 從加鎖的對象的監視器(監視器就是鎖)的等待隊列中, 任意取出一個等待線程, 讓該線程處於runnable狀態;
wait()的意思是: 把鎖住對象的當前線程, 放入到監視器的等待隊列中。
所以,notify()的意思就是要喚醒等待隊列中的一個等待線程,當程序發起這么一個事件後, 是由虛擬機的線程調度器完成線程狀態之間的轉換的。
❻ 用Java程序完成以下場景(用繼承多態):有一個主人(Master類),他養了兩只寵物(Pet類)
publicclassRun{
publicstaticvoidmain(String[]args){
Mastermaster=newMaster();
master.feedDog("雞骨頭");
master.feedCat("雞骨頭");
}
}
classMaster{
privatePetmPet;
privateFoodmFood;
publicvoidfeedCat(Stringfood){
mPet=newCat();
mFood=newFood(food);
mPet.eat(mFood);
}
publicvoidfeedDog(Stringfood){
mPet=newDog();
mFood=newFood(food);
mPet.eat(mFood);
}
}
classDogextendsPet{
@Override
publicvoideat(Foodfood){
System.out.println("正在喂小狗吃"+food.getFood());
if(food.getFood().matches(Food.BONE)){
System.out.println("小狗正在吃"+food.getFood()+"!");
}else{
System.out.println("但是小狗不喜歡吃"+food.getFood()+"!");
}
}
}
classCatextendsPet{
@Override
publicvoideat(Foodfood){
System.out.println("正在喂小貓吃"+food.getFood());
if(food.getFood().matches(Food.FISH)){
System.out.println("小貓正在吃"+food.getFood()+"!");
}else{
System.out.println("但是小貓不喜歡吃"+food.getFood()+"!");
}
}
}
classFood{
publicfinalstaticStringBONE=".*骨.*";
publicfinalstaticStringFISH=".*魚.*";
privateStringfood;
publicStringgetFood(){
returnfood;
}
publicvoidsetFood(Stringfood){
this.food=food;
}
publicFood(Stringfood){
this.food=food;
}
}
classPet{
publicvoideat(Foodfood){
}
}