类和对象,方法和属性

1.什么是类:类是由一组相同的属性和方法的对象组成的集合。

2.什么事对象:对象是用来描述客观事物的一个实体,由一组属性和方法构成的。

3.方法和属性的概念:3.1属性对象具有的各种特征。

                                 3.2方法是对象执行的操作。

4.什么是方法重载:1.同一个类中方法名一样2.参数列表不一样:参数个数不同,参数类型不同,参数顺序不同.

例如:

public class Test {
	
	 void test(){                                   //test()方法第一次重载,没有参数
         System.out.println("No parameters");
  }
  void test(int a){                             //test()方法第二次重载,一个整型参数
         System.out.println("a: "+a);
  }
  void test(int a,int b){                        //test()方法第三次重载,两个整型参数
         System.out.println("a and b: "+a+" "+b);
  }
  double test(double a){                     //test()方法第四次重载,一个双精度型参数
         System.out.println("double a: "+a);
         return a*a;                        //返回a*a的值
  }

		 public static void main(String args[]){
             Test ob=new Test();
             double result;
             ob.test();            //定义的是test()方法
             ob.test(10);             //定义的是test(int a)方法
             ob.test(10,20);            //定义的是test(int a,int b)方法
             result=ob.test(123.23);       //定义的是test(double a)方法
             System.out.println("result of ob.test(123.23): "+result);   //输出result的值
      }

5.实参和形参:调用方法的时候·,传递的参数叫做实参;定义方法的时候,括号里面的参数叫做形参。

6.Eclipse常用的快捷键:Ctrl shift f 代码格式化(有可能被输入法占用);Ctrl shift o 导包;Alt / 代码提示;/** enter 注释、



就到这里吧。

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

                  构造方法要与类名相同,可承载多个个不同的方法,且构造方法没有返回值。

例如:1.

/**
 * 测试类
 * 
 * @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;
	}
}
2.
/**+
 * 
 * 动物练习
 * @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 +" 斤");
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值