/**
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、网站空间、营销软件、网站建设、吴中网站维护、网站推广。
* 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 回车。
---------------就可以了!
代码如下:
第一个
class Animal {
private int age;
private String name;
public Animal(int age, String name) {
this.age = age;
this.name = name;
}
public void print() {
System.out.println("年龄:" + age);
System.out.println("名称:" + name);
}
}
public class Test {
public static void main(String[] args) {
Animal a1 = new Animal(3, "小猫");
a1.print();
Animal a2 = new Animal(2, "小狗");
a2.print();
}
}
第二个:
class Animal {
private int age;
private String name;
public Animal(int age, String name) {
this.age = age;
this.name = name;
}
public void print() {
System.out.println("年龄:" + age);
System.out.println("名称:" + name);
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Bird extends Animal {
public Bird(int age) {
super(age, "小鸟");
}
public void print() {
System.out.println("年龄:" + getAge());
System.out.println("名称:" + getName());
}
}
class Fish extends Animal {
public Fish(int age) {
super(age, "鱼");
}
public void print() {
System.out.println("年龄:" + getAge());
System.out.println("名称:" + getName());
}
}
public class Test2 {
public static void main(String[] args) {
Animal[] animals = {
new Bird(2),
new Fish(10),
new Bird(3),
new Bird(4),
new Fish(5)
};
for (Animal animal : animals) {
animal.print();
}
}
}
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;
}
}
小伙儿 看行不 行的话 最佳啊!!分!分!