java中类的概念:
类是具有相同属性和方法的一组对象的集合
java中对象的概念:
用来描述客观事物的一个实体,由一组属性和方法构成(对象是一个具体事物,看得见,摸得着)
java中的方法和属性概念:
属性:对象具有的各种特征
方法:对象执行的操作
方法重载:
重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变;
参数列表必须完全与被重写方法的相同;
返回类型必须完全与被重写方法的返回类型相同。
public class CZ {
public void writeessay() {
}
public void writeessay(String pen,String book) {
System.out.println("小明拿着"+pen+"在"+book+"写字");
}public String writeessay(String computer,int game) {
System.out.println("小明拿着"+computer+"在"+game+"打游戏");
return("0");
}
}
import java.util.Scanner;
public class Text {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String song = sc.nextLine();
XiaoMing xiaoming = new XiaoMing();
xiaoming.sing(song);
xiaoming.play("星际战甲");
xiaoming.cook();
CZ essay = new CZ();
essay.writeessay("派克钢笔","笔记本");
essay.writeessay("外星人电脑","桌子上");
}
}
Eclipse常用快捷键:
导包:Ctrl+Shift+O
代码格式化:Ctrl+Shift+F
删除当前行:Ctrl+D
代码提示:ALT+/
对象的属性:
public class ShuXing {
int ShuXingage;
public ShuXing(String name) {
System.out.println("姓名:" + name);
}
public void drive() {
}
public void park() {
}
public void setage(int age) {
ShuXingage = age;
}
public int getage() {
System.out.println("年龄:" + ShuXingage);
return ShuXingage;
}
public static void main(String[] args) {
ShuXing s = new ShuXing("王波");
s.setage(22);
s.getage();
}
}
构造方法:
构造方法是一种特殊的方法。主要功能是用来在创建对象时初始化对象,为对象成员变量赋初始值;构造方法与类名相同,可重载多个不同的构造方法
构造方法的方法名必须与类名相同。
构造方法没有返回类型,也不能第一位void,在方法名前不声明方法类型。
构造方法的主要作用是完成对象的初始化工作
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;
}
}
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 +" 斤");
}
}