Java习题——面向对象基础

1.定义一个汽车类(Car),属性有颜色,品牌,车牌号,价格,并实例化两个对象,给属性赋值,并输入属性值

//1.定义一个汽车类(Car),属性有颜色,品牌,车牌号,价格,并实例化两个对象,给属性赋值,并输入属性值
package cu.edu.nefu1;

public class Car {
	String color;
	String brand;
	String ID;
	double price;
	public static void main(String[] args) {
		Car car1 = new Car();
		car1.brand = "奥迪";
		car1.color = "白色";
		car1.ID = "黑A888888";
		car1.price = 2341.12;
		System.out.println(car1.brand+" "+car1.color+" "+car1.ID+" "+car1.price);
	}
}

2.定义一个球员类(Player),属性有身高,体重,姓名,实例化两个球员,分别是姚明和科比;

//2.定义一个球员类(Player),属性有身高,体重,姓名,实例化两个球员,分别是姚明和科比;
package cu.edu.nefu1;

public class Player {
	String name;
	double height;
	double weight;
	
	public static void main(String[] args) {
		Player player1 = new Player();
		player1.name = "姚明";
		player1.height = 2.3;
		player1.weight = 75;
		Player player2 = new Player();
		player2.name = "科比";
		player2.height = 2.1;
		player2.weight = 80;
		System.out.println(player1.name+"身高"+player1.height+"米,体重"+player1.weight+"公斤");
		System.out.println(player2.name+"身高"+player2.height+"米,体重"+player2.weight+"公斤");
	}
}

3.定义一个僵尸类(Zombie),属性有名子,体力值,攻击力,实例化三个僵尸类,并给属性赋值;

//3.定义一个僵尸类(Zombie),属性有名子,体力值,攻击力,实例化三个僵尸类,并给属性赋值;
package cu.edu.nefu1;

public class Zombie {
	String name;
	int power;
	int attack;
	public static void main(String[] args) {
		Zombie zombie1 = new Zombie();
		zombie1.name = "普通僵尸";
		zombie1.power = 2000;
		zombie1.attack = 2000;
		Zombie zombie2 = new Zombie();
		zombie2.name = "铁桶僵尸";
		zombie2.power = 3000;
		zombie2.attack = 3000;
		Zombie zombie3 = new Zombie();
		zombie3.name = "盗贼僵尸";
		zombie3.power = 4000;
		zombie3.attack = 4000;
		System.out.println("名字\t"+"体力值\t"+"攻击力\t");
		System.out.println(zombie1.name+"\t"+zombie1.power+"\t"+zombie1.attack+"\t");
		System.out.println(zombie2.name+"\t"+zombie2.power+"\t"+zombie2.attack+"\t");
		System.out.println(zombie3.name+"\t"+zombie3.power+"\t"+zombie3.attack+"\t");
	}
}

4.打印int,char, float,double,String ,boolean这些数据类型作为类属性时的默认值

//4.打印int,char, float,double,String ,boolean这些数据类型作为类属性时的默认值
package cu.edu.nefu1;

public class Default {
	int i;
	char ch;
	float f;
	double d;
	String st;
	boolean b;
	public static void main(String[] args) {
		Default exam = new Default();
		System.out.println("int类型的默认值为:"+exam.i);
		System.out.println("char类型的默认值为:"+exam.ch);
		System.out.println("float类型的默认值为:"+exam.f);
		System.out.println("double类型的默认值为:"+exam.d);
		System.out.println("String类型的默认值为:"+exam.st);
		System.out.println("boolean类型的默认值为:"+exam.b);
	}
}

 

你可能感兴趣的:(Java习题——面向对象基础)