我写了一个,内容比较简单的。代码如下:public class AnimalTest {
专注于为中小企业提供成都网站设计、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业铁西免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
Animal animal;
public void eat(Animal animal){
animal.eat();
}
public void walk(Animal animal){
animal.walk();
}
public static void main(String args[]){
Animal animal=new Animal("animal");
Wolf w=new Wolf("wolf");
Goat g=new Goat("goat");
AnimalTest at=new AnimalTest();
at.eat(animal);
at.eat(w);
at.eat(g);
at.walk(animal);
at.walk(w);
at.walk(g);
}
}
class Animal {
String name;
public Animal(String name){
this.name=name;
}
public Animal(){}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void eat(){
System.out.println("animal eat");
}
public void walk(){
System.out.println("animal walk");
}
public String toString(){
return name;
}
}class Wolf extends Animal {
public Wolf(String name){
super(name);
}
public Wolf(){}
public void eat(){
System.out.println("wolf eat meat");
}
public void walk(){
System.out.println("wolf walk");
}
public String toString(){
return name;
}
}class Goat extends Animal {
public Goat(String name){
super(name);
}
public Goat(){}
public void eat(){
System.out.println("goat eat grass");
}
public void walk(){
System.out.println("goat walk");
}
public String toString(){
return name;
}
}
abstract class Animal{
String type;
public Animal(String type){
this.type=type;
}
public abstract void Talk();
public void showInfo(){Talk();}
}
class Dog extends Animal{
String name;
public Dog(String type,String name){
super(type);
this.name=name;
}
public void Talk(){
System.out.println("此动物是:"+name+",此动物属于:"+type);
}
}
class Cat extends Animal{
String name;
public Cat(String type,String name){
super(type);
this.name=name;
}
public void Talk(){
System.out.println("此动物是:"+name+",此动物属于:"+type);
}
}
public class Diy_5_2_1{
public static void main(String[] args){
Dog doggie=new Dog("犬科动物","德国黑贝");
Cat kitty=new Cat("猫科动物","波斯猫");
doggie.showInfo();
kitty.showInfo();
}
}
import java.util.ArrayList;
import java.util.*;
public class BianHao {
/**
*
*/
public static void main(String[] args) {
//a字符串数组 length=5
String[] a = { "狗", "猪", "兔", "鸡", "羊" };
//b数组 length=20
int[] b = new int[20];
//设置标志位
boolean flag=false;
//产生随机数 并判断是否有重复的数值,如果flag=true说明产生的随机数没有重复的 退出wile循环
while(flag==false){
for (int i = 0; i b.length; i++) {
b[i]=(int)(Math.random()*62+1);
}
//判断每个元素的值是否相等 如果有就为false 从新产生随机数
flag=pd(b);
}
//对数组b进行排序,按升序
Arrays.sort(b);
//创建c数组
ListString c=new ArrayListString();
for (int j = 0; j a.length; j++) {
for (int h = 0; h b.length; h++) {
//对c数组进行填充值
c.add(a[j] + b[h] + "");
}
}
//对c数组进行遍历
for (String str : c) {
System.out.print(str+",");
}
}
public static boolean pd(int[] arr) {
//按位比较
for (int k = 0; k arr.length - 1; k++) {
for (int j = k + 1; j arr.length; j++) {
if (arr[k] == arr[j]) {
return false;
}
}
}
return true;
}
}
小伙儿 看行不 行的话 最佳啊!!分!分!
/**
* Animal.java
*/
package animal;
public class Animal {
private String name; //名称
private int bodySize; //大小
private float weight; //体重
public Animal (String name, int bodySize, float weight) {
this.name = name;
this.bodySize = bodySize;
this.weight = weight;
}
public void run () { //跑
System.out.println("I'm running!");
}
public void jump () { //跳
System.out.println("I'm jumping!");
}
public void walk () { //走
System.out.println("I'm walking!");
}
public String getName () {
return name;
}
public int getBodySize () {
return bodySize;
}
public int getWeight () {
return weight;
}
public String toString () {
return ("I'm a " + name +
", my body size is " + bodySize +
", and my weight is " + weight + ".");
}
}
/**
* 这是一个测试类: TestAnimal.java
*/
package animal;
public class TestAnimal {
public static void main (String args[]) {
Animal ani = new Animal("tiger", 5, 10.5);
System.out.println(ani.toString());
ani.run();
ani.jump();
ani.walk();
}
}
------------------------------------
输出:
I'm a tiger, my body size is 5, my weight is 10.5.
I'm running!
I'm jumping!
I'm walking!
------------------------------------
不会测试就不测试呗,我输出都帮你写出来了。
------------------------------------
测试结果就是上面的内容。把以上两个程序保存成 Animal.java 和 TestAnimal.java。放入一个新建名为 animal 的文件夹。比如路径是 C:\animal\,运行 cmd,输入:cd\ 回车,
再输入:C: 回车,
然后输入:javac animal\Animal.java animal\TestAnimal.java 回车,
如果没有什么错误提示,再输入:java animal.TestAnimal 回车。
---------------就可以了!