1.什么是对象?
答:是用来描述客观事物的一种实体,它是由一组属性和方法构成。
2.什么是类?
答:相同的属性和方法组成的一组对象集合。
3.方法和属性是什么?
答:方法:对象所执行的操作。属性:对象所具有的特征。
4.什么是方法重载?(代码)
public class Text {
public void writeEssay(String a,String b) {
System.out.println("用钢笔"+"在纸上写");
}
public void writeEssay(int a,String b) {
System.out.println("用"+a+"张A4纸写");
}
public void writeEssay(String a,int b) {
System.out.println("用"+6+"支钢笔");
}
public String writeEssay(String computer) {
return computer;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Text s = new Text();
s.writeEssay("","");
s.writeEssay(5,"");
s.writeEssay("",6);
String aaa = "联想电脑";
String c = s.writeEssay(aaa);
System.out.println(c);
}
}
5.Eclipse常用快捷键有哪些?
导包:Ctrl+shift+O,内容格式化:Ctrl+shift+F,自动不全代码或者提示代码:alt+/,自动注释所选行代码、取消注释:ctrl+/
实训Day4_构造方法:
2.)方法的重载:1.方法名一样。 2.参数列表不一样(参数个数不同或参数类型不用)。
3.)JRE 和 JDK 分别是什么意思?
JRE:Java Runtime Environmengt :java运行时环境,包括java虚拟机和java基础库。
JDK:java Development Kit : java开发工具包。
4.)怎样理解JAVA的平台可移植性?
java被编译成字节码,字节码与平台无关只面向JVM编写,因为java有一个JVM(Java Virtual Machine)java虚拟机,不同操作平台有自己的JVM,所以java可以跨平台运行。
构造方法例题1:
public class Ex_animal {
public static void main(String[] args) {
// TODO Auto-generated method stub
Animal an1 = new Animal("兔子","草",5);
Animal an2 = new Animal();
Animal an3 = new Animal(an1,an2);
an2.all(an3);
}
}
class Animal{
String name;
String eat;
int weight;
public Animal() {
this.name = name;
this.eat = "猪蹄";
this.weight = 12;
}
public Animal(Animal a,Animal b) {
this.name = b.name;
this.eat = a.eat;
this.weight = b.weight;
}
public Animal(String name,String eat,int weight) {
this.name = name;
this.eat = eat;
this.weight = weight;
}
public Animal(String name,int weight) {
this.name = name;
this.weight = weight;
}
public void eat1() {
System.out.println("这个动物喜欢吃" + this.eat);
}
public void run() {
System.out.println("这个动物会跑");
}
public void all(Animal an) {
System.out.println("这是一只 " + this.name + ",它喜欢吃 " + an.eat + ",它重 " + an.weight +" 斤");
}
}
构造方法例题2:
/**
* 测试类
*
* @author lenovo64
*这只小狗是小白,白色正在和那只小猫叫做小黑,黑色在打架
*获胜的动物是黑色
*/
public class Test {
public static void main(String[] args) {
Animal dog = new Animal("小黑", "小猫", "黑色");
Animal cat = new Animal("小白", "小狗", "白色");
String s = dog.play(cat, dog);
System.out.println("获胜的动物是" + s);
}
}
class Animal {
String name;
String kind;
int age;
String color;
long animalID;
String date;
public Animal(String name, String kind) {
this.name = name;
this.kind = kind;
}
public Animal(String name, String color, String kind) {
this.name = name;
this.color = color;
this.kind = kind;
}
public Animal(String name, int age, long animalID) {
this.age = age;
this.animalID = animalID;
}
public String play(Animal dog, Animal cat) {
System.out.println("这只" + dog.color + "是" + dog.name + "," + dog.kind + ",正在和那只" + cat.color + "叫做" + cat.name
+ "," + cat.kind + ",在打架");
return cat.kind;
}
}