1.什么是类?
是一个抽象的类,是具有相同方法和属性的一组对象的集合。
2.什么是对象?
用来描述客观事物的一个实体,有一组属性和方法构成。
3.方法和属性是什么?
方法:对象具有的行为
属性:对象具有的特征
4.什么是方法重载?
对同一个命名空间,不能定义同样的方法,即方法名字,参数个数,参数类型都相同。
若是名字相同,但是参数个数或者参数类型或者顺序不同则构成方法重载,与返回值无关。但是可以在不同的类中定义相同的方法
代码:
public class ChongZai {
public void eat() {
System.out.println("无参");
}
public void eat(int b, String a) {
System.out.println("吃了" + b + "盘");
}
public void eat(String b, int a) {
System.out.println("吃了" + a + "pan");
}
public void eat(int a, int b) {
System.out.println("吃了" + a + "ge" + b);
}
public String eat(String a, String b) {
return a;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ChongZai cz = new ChongZai();
cz.eat(5, 2);
cz.eat(3, "wuhuarou");
cz.eat("rou", 3);
String a = cz.eat("rou", "cai");
System.out.println(a);
}
}
5.Eclipse常用的快捷键是什么?
ctrl+shift+r (打开资源)
ctrl+o (快速outline)
“/**”+回车 或 ctrl+/ (需要注释的)
Alt+/ (智能提醒)
"\t" (制表符)
ctrl+shift+o (导包)
ctrl+shift+f (代码格式)
6.其他
(1)包名规则:必须要小写字母
通常用公司域名倒置
(2)类名规则:首字母必须大写采用驼峰命名
(3)变量定义规则:首字母必须小写
采用驼峰式命名
(4)对象在代码中的定义:Book sanGuo = new book
类名 对象名 实例化
7.什么是形参?什么是实参?
形参就是在函数定义体中的参数;实参就是在调用函数时实际传进函数的参数。
例子:
int f(int b) //b为形参
{return b;}
main()
{int a=1;
printf("%d",f(a)); //a为实参
}
8.java的构造方法
(1)构造方法的格式:方法名必须与类名相匹配:不要声明返回类型(即使返回类型是void,也不是构造方法)。
(2)this和super的使用:
“this”用法:实例方法中使用this关键字,它指向正在执行的方法的类的实例对象
构造方法中通过使用this关键字调用其他构造方法是,那么这句代码必须放在第一行,否则会编译错误。
构造方法中只能通过this调用一次其他的构造方法。
“super”用法:实例方法和构造方法中的super关键字都指向父类,实例方法中的super关键字是去调用父类当中的某个方法。
使用super调用父类构造器的语句必须是子类构造器的第一条语句。
如果子类构造器没有显式地调用父类的构造器,则将自动调用父类的默认(没有参数)的构造器。如果父类没有不带参数的构造器,并且在子类的构造器中又没有显式地调用父类的构造器,则java编译器将报告错误。
(3)总结:
构造方法必须与类同名(如果一个源文件中有多个类,那么构造方法必须与公共类同名)
每个类可以有一个以上的构造方法
构造方法可以有0个、1个或1个以上的参数
构造方法没有返回值
构造方法总是伴随着new操作一起调用
9.代码
(1)
package com.zjm.www.day10;
/**+
*
* 动物练习
* @author Zjm
*
*/
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)
package com.lenovo.www.day10;
/**
* 测试类
*
* @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;
}
}