JAVA练习2—类,对象,方法重载

什么是类?

类是具有相同属性和方法的一组对象的集合。

什么是对象?

用来描述客观事物的一个实体,由一组属性和方法构成。

方法和属性是什么?

方法:对象执行的操作。

属性:对象具有的各种特征。

什么是方法重载?(附代码)

1. 方法名一样。

2. 参数列表不一样(参数个数不同或者参数类型不同)。

public class Essay {
	public void writeEssay() {
		System.out.println("无参方法");
	}
	public void writeEssay(String pencil,String paper) {
		System.out.println("用笔和纸写文章");
	}
	public void writeEssay(int a,String b) {
		System.out.println("用"+a+"支笔写文章");
	}
	public void writeEssay(String a,int b) {
		System.out.println("用"+b+"张纸写文章");
	}
	public String writeEssay(String computer) {
		computer = computer+"写文章";
		return computer;
	}

}




public class Test {

	public static void main(String[] args) {
Essay essay = new Essay();
		essay.writeEssay("英雄", "A4");
		essay.writeEssay(10, "aaa");
		String abc = "联想电脑";
		String s = essay.writeEssay(abc);
		System.out.println(s);
  }

Eclipse常用快捷键有哪些?

导包快捷键:Shift+Ctrl+O

多行注释快捷键:Ctrl+/

代码格式化快捷键:Shift+Ctrl+F

保存快捷键:Ctrl+S。

其它

方法的定义:

JAVA中,用方法来描述现实事物的行为。

JDKJRE分别是什么意思?

JDK: 开发人员使用的软件开发包。

JRE: Java的运行环境,是面向Java程序的使用者,而不是开发者。

如何理解JAVA的可移植性(跨平台)?

JAVA在运行过程中把代码编译成字节码在JVM上运行的,JVM是可跨平台的。

构造方法:(附代码)

是一种特殊的方法,其主要功能是用来在创建对象时初始化对象,即为对象成员变量赋初始值。

构造方法与类名相同,可以重载多个不同的构造方法。

构造方法没有返回类型,也不能定义为void,在方法名面前不能声明方法类型。

动物类:
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:
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;
	}
}




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值