`
x125521853
  • 浏览: 71377 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

第一章 类与方法简介

阅读更多

一:类

    理解类的最重要的事情就是它定义了一种新的数据类型。一旦定义后,就可以使用这个新类型创建该类型的对象.因此,类是对象的模板,而对象就是类的实例,因为对象是类的一个实例。

    类是一种自定义的抽象的数据类型,可以想像成某种类型事物的统称,比如, 小张,小李,小赵都是学生,那到底学生是个什么东西呢,我们可以想像一样,学生就是具有姓名,年龄,班级,学习成绩,需要上学,放学,写作业的一类抽象事物的统称.

    面向对象的思想将现实世界中的客观事物描述成具有属性和行为的对象,通过抽象出同一类对象的共同属性和行为,形成类.

    比如说学生有姓名, 性别, 年龄, 语文成绩, 数学成绩, 英文成绩这些特点, 那我们在程序中怎么表示这些特点呢, 小张, 小李, 小赵这三个学生又怎么表示呢.

   //示例1:(StudentDemo01.java):   

class Student
{
	String stdName; //学生姓名
	String stdSex; //学生性别
	int stdAge; //学生年龄
	double chineseMark; //语文成绩
	double mathMark; //数学成绩
	double englishMark; //英文成绩
}

class StudentDemo01
{
	public static void main(String[] args)
	{
		//实例化学生小张
		//Student stdZhang = new Student();
		Student stdZhang;
		stdZhang = new Student();
		//初始化各个属性
		stdZhang.stdName = "小张";
		stdZhang.stdSex = "男";
		stdZhang.stdAge = 20;
		stdZhang.chineseMark = 98;
		stdZhang.mathMark = 99;
		stdZhang.englishMark = 100;
		System.out.println("自我介绍");
		System.out.println("我的名字是:" + stdZhang.stdName);
		System.out.println("我的性别是:" + stdZhang.stdSex);
		System.out.println("我的年龄是:" + stdZhang.stdAge);
		System.out.println("我的语文成绩是:" + stdZhang.chineseMark);
		System.out.println("我的数学成绩是:" + stdZhang.mathMark);
		System.out.println("我的英文成绩是:" + stdZhang.englishMark);
		System.out.println();

		//实例化学生小李
		//Student stdLi = new Student();
		Student stdLi;
		stdLi = new Student();
		//初始化各个属性
		stdLi.stdName = "小李";
		stdLi.stdSex = "女";
		stdLi.stdAge = 18;
		stdLi.chineseMark = 88;
		stdLi.mathMark = 89;
		stdLi.englishMark = 90;
		System.out.println("自我介绍");
		System.out.println("我的名字是:" + stdLi.stdName);
		System.out.println("我的性别是:" + stdLi.stdSex);
		System.out.println("我的年龄是:" + stdLi.stdAge);
		System.out.println("我的语文成绩是:" + stdLi.chineseMark);
		System.out.println("我的数学成绩是:" + stdLi.mathMark);
		System.out.println("我的英文成绩是:" + stdLi.englishMark);
		System.out.println();

		//实例化学生小赵
		//Student stdZhao = new Student();
		Student stdZhao;
		stdZhao = new Student();
		//初始化各个属性
		stdZhao.stdName = "小赵";
		stdZhao.stdSex = "女";
		stdZhao.stdAge = 22;
		stdZhao.chineseMark = 78;
		stdZhao.mathMark = 79;
		stdZhao.englishMark = 70;
		System.out.println("自我介绍");
		System.out.println("我的名字是:" + stdZhao.stdName);
		System.out.println("我的性别是:" + stdZhao.stdSex);
		System.out.println("我的年龄是:" + stdZhao.stdAge);
		System.out.println("我的语文成绩是:" + stdZhao.chineseMark);
		System.out.println("我的数学成绩是:" + stdZhao.mathMark);
		System.out.println("我的英文成绩是:" + stdZhao.englishMark);
		System.out.println();

	}
}

    可以看到声明类要使用class关键词,在一个class内定义的数据或变量称为成员变量.   

   可以看到我这里把两个class写在了一个文件中.

    一个是Student,它是抽象数据模型类

    一个是StudentDemo01. 它是程序运行类.

 

二:创建对象

   1.当创建一个类时,就创建了一个新的数据类型,可以使用该类型来声明对象,实际上,获取类的对象是一个分为两步的过程:

   首先,必须声明该类型的一个引用,这个引用不定义一个对象,它只是一个可以引用一个对象的引用,
   其次,必须为该对象分配实际的物理内存,就是用new关键字来分配内存, 然后将引用和分配的内存进行绑定.

   2.类和对象之间的区别:类创建一种新的数据类型, 可以使用这种新的数据类型来创建对象,也就是类创建一个定义它的成员之间关系的逻辑框架.当声明一个类的对象时,就是正在创建该类的一个实例,因此,类是一个逻辑结构,而对象具有物理真实性(即对象在内存中占有空间).

   3.Student std1 = new Student();
     Student std2 = std1;

 

     此时,std1std2指向同一个对象为null

      null 非常特殊,它是万能对象,可以表示任何对象, 它在内在中不存在内存空间, null用来表示一个引用没有实例存在.

      String str1 = null; str1是null,那么内存根本没创建字符串对像
      String str2 = ””; 确实存在一个由str2引用的字符串对像,只不过这个字符串的值是""
 .

 

三:方法介绍

   1.type name(parameterList){
       ...方法的实现
     }

     其中type指定了该方法返回的数据类型. 这可以是任何有效的类型, 包括创建的类的类型,如果该方法不返回值, 它的返回类型一定得是void。

     方法名是由name指定,可以自己定义,但是得有意义,不能定义成a,b,c等等无意义的名字.

     parameterList参数列表是一个用逗号分开的类型和标识符序列,参数本质上是变量,对应于方法被调用时所需要的实际参数.如果方法没有参数,则参数列表将为空.

     返回类型不是void的方法将使用下面的形式return语句把一个值返回给调用程序.
     return value;  其中value是返回的值.

   2.没有返回值,没有参数列表的方法

  //示例2:StudentDemo02.java  

class Student
{
	String stdName; //学生姓名
	String stdSex; //学生性别
	int stdAge; //学生年龄
	double chineseMark; //语文成绩
	double mathMark; //数学成绩
	double englishMark; //英文成绩

	void introductionSelf()
	{
		System.out.println("自我介绍");
		System.out.println("我的名字是:" + stdName);
		System.out.println("我的性别是:" + stdSex);
		System.out.println("我的年龄是:" + stdAge);
		System.out.println("我的语文成绩是:" + chineseMark);
		System.out.println("我的数学成绩是:" + mathMark);
		System.out.println("我的英文成绩是:" + englishMark);
		System.out.println();
	}
}

class StudentDemo02
{
	public static void main(String[] args)
	{
		//实例化学生小张
		//Student stdZhang = new Student();
		Student stdZhang;
		stdZhang = new Student();
		//初始化各个属性
		stdZhang.stdName = "小张";
		stdZhang.stdSex = "男";
		stdZhang.stdAge = 20;
		stdZhang.chineseMark = 98;
		stdZhang.mathMark = 99;
		stdZhang.englishMark = 100;
		stdZhang.introductionSelf();		

		//实例化学生小李
		//Student stdLi = new Student();
		Student stdLi;
		stdLi = new Student();
		//初始化各个属性
		stdLi.stdName = "小李";
		stdLi.stdSex = "女";
		stdLi.stdAge = 18;
		stdLi.chineseMark = 88;
		stdLi.mathMark = 89;
		stdLi.englishMark = 90;
		stdLi.introductionSelf();

		//实例化学生小赵
		//Student stdZhao = new Student();
		Student stdZhao;
		stdZhao = new Student();
		//初始化各个属性
		stdZhao.stdName = "小赵";
		stdZhao.stdSex = "女";
		stdZhao.stdAge = 22;
		stdZhao.chineseMark = 78;
		stdZhao.mathMark = 79;
		stdZhao.englishMark = 70;
		stdZhao.introductionSelf();
	}
}

   这里的void是没有返回值, introductionSelf()是方法名, 括号里没有参数列表,则说明这个方法不需要传递参数.大括号内的内容是方法的实现部分.
然后我们在程序中调用时可以通过stdZhang.introductionSelf(); 来调用此方法.

 

   3.有参数列表,没有返回值的方法的写法

  //示例3:StudentDemo03.java  

class Student
{
	String stdName; //学生姓名
	String stdSex; //学生性别
	int stdAge; //学生年龄
	double chineseMark; //语文成绩
	double mathMark; //数学成绩
	double englishMark; //英文成绩

	//自我介绍功能
	void introductionSelf()
	{
		System.out.println("自我介绍");
		System.out.println("我的名字是:" + stdName);
		System.out.println("我的性别是:" + stdSex);
		System.out.println("我的年龄是:" + stdAge);
		System.out.println("我的语文成绩是:" + chineseMark);
		System.out.println("我的数学成绩是:" + mathMark);
		System.out.println("我的英文成绩是:" + englishMark);
		System.out.println();
	}

	//演示this关键字的作用
	void initSelf(String _stdName,String _stdSex,int _stdAge, double _chineseMark,double _mathMark,double _englishMark)
	{		
		stdName = _stdName;
		stdSex = _stdSex;
		stdAge = _stdAge;
		chineseMark = _chineseMark;
		mathMark = _mathMark;
		englishMark = _englishMark;
		
	}
}

class StudentDemo03
{
	public static void main(String[] args)
	{
		//实例化学生小张
		//Student stdZhang = new Student();
		Student stdZhang;
		stdZhang = new Student();
		stdZhang.initSelf("小张","男",28, 98D,95D,97D);
		stdZhang.introductionSelf();
		

		//实例化学生小李
		//Student stdLi = new Student();
		Student stdLi;
		stdLi = new Student();
		//初始化各个属性
		stdLi.initSelf("小李","女",28, 88D,85D,87D);
		stdLi.introductionSelf();

		//实例化学生小赵
		//Student stdZhao = new Student();
		Student stdZhao;
		stdZhao = new Student();
		//初始化各个属性
		stdZhao.initSelf("小赵","女",22, 78D,75D,77D);
		stdZhao.introductionSelf();

	}
}

   

    4.有返回值,没有参数列表的方法的写法.

  //示例4:StudentDemo04.java  

class Student
{
	String stdName; //学生姓名
	String stdSex; //学生性别
	int stdAge; //学生年龄
	double chineseMark; //语文成绩
	double mathMark; //数学成绩
	double englishMark; //英文成绩

	//自我介绍
	void introductionSelf()
	{
		System.out.println("自我介绍");
		System.out.println("我的名字是:" + stdName);
		System.out.println("我的性别是:" + stdSex);
		System.out.println("我的年龄是:" + stdAge);
		System.out.println("我的语文成绩是:" + chineseMark);
		System.out.println("我的数学成绩是:" + mathMark);
		System.out.println("我的英文成绩是:" + englishMark);
		
	}

	//初始化
	void initSelf(String stdName,String stdSex,int stdAge, double chineseMark,double mathMark,double englishMark)
	{		
		this.stdName = stdName;
		this.stdSex = stdSex;
		this.stdAge = stdAge;
		this.chineseMark = chineseMark;
		this.mathMark = mathMark;
		this.englishMark = englishMark;
		
	}

	//算成绩平均分的功能
	double markAvg()
	{
		double mark = 0D;
		mark = (chineseMark + mathMark + englishMark)/3;
		return mark;
	}
}

class StudentDemo04
{
	public static void main(String[] args)
	{
		double mark = 0; //定义变量,接收平均分
		//实例化学生小张
		//Student stdZhang = new Student();
		Student stdZhang;
		stdZhang = new Student();
		stdZhang.initSelf("小张","男",28, 98D,98D,98D);
		stdZhang.introductionSelf();
		mark = stdZhang.markAvg();
		System.out.println("我的成绩平均分是:" + mark);
		//System.out.println("我的成绩平均分是:" + stdZhang.markAvg());
		System.out.println();

		//实例化学生小李
		//Student stdLi = new Student();
		Student stdLi;
		stdLi = new Student();
		//初始化各个属性
		stdLi.initSelf("小李","女",28, 88D,88D,88D);
		stdLi.introductionSelf();
		System.out.println("我的成绩平均分是:" + stdLi.markAvg());
		System.out.println();

		//实例化学生小赵
		//Student stdZhao = new Student();
		Student stdZhao;
		stdZhao = new Student();
		//初始化各个属性
		stdZhao.initSelf("小赵","女",22, 78D,78D,78D);
		stdZhao.introductionSelf();
		System.out.println("我的成绩平均分是:" + stdZhao.markAvg());
		
	}
}

 

    5.有返回值,有参数列表的方法的写法

  //示例5:StudentDemo05.java  

class Student
{
	String stdName; //学生姓名
	String stdSex; //学生性别
	int stdAge; //学生年龄
	double chineseMark; //语文成绩
	double mathMark; //数学成绩
	double englishMark; //英文成绩

	//自我介绍
	void introductionSelf()
	{
		System.out.println("自我介绍");
		System.out.println("我的名字是:" + stdName);
		System.out.println("我的性别是:" + stdSex);
		System.out.println("我的年龄是:" + stdAge);
		System.out.println("我的语文成绩是:" + chineseMark);
		System.out.println("我的数学成绩是:" + mathMark);
		System.out.println("我的英文成绩是:" + englishMark);
		
	}

	//初始化
	void initSelf(String stdName,String stdSex,int stdAge, double chineseMark,double mathMark,double englishMark)
	{		
		this.stdName = stdName;
		this.stdSex = stdSex;
		this.stdAge = stdAge;
		this.chineseMark = chineseMark;
		this.mathMark = mathMark;
		this.englishMark = englishMark;
		
	}

	//算成绩平均分的功能
	double markAvg()
	{
		double mark = 0D;
		mark = (chineseMark + mathMark + englishMark)/3;
		return mark;
		//return (chineseMark + mathMark + englishMark)/3;
	}

	//平均成绩的比较方法,
	boolean markEquals(Student std)
	{
		return this.markAvg() > std.markAvg();
	}
}

class StudentDemo04
{
	public static void main(String[] args)
	{		
		//实例化学生小张
		//Student stdZhang = new Student();
		Student stdZhang;
		stdZhang = new Student();
		stdZhang.initSelf("小张","男",28, 98D,98D,98D);
		stdZhang.introductionSelf();
		double mark = stdZhang.markAvg();//接收平均分的返回值
		System.out.println("我的成绩平均分是:" + mark);
		//System.out.println("我的成绩平均分是:" + stdZhang.markAvg());		
		System.out.println();

		//实例化学生小李
		//Student stdLi = new Student();
		Student stdLi;
		stdLi = new Student();
		//初始化各个属性
		stdLi.initSelf("小李","女",28, 88D,88D,88D);
		stdLi.introductionSelf();
		System.out.println("我的成绩平均分是:" + stdLi.markAvg());
		System.out.println();

		//实例化学生小赵
		//Student stdZhao = new Student();
		Student stdZhao;
		stdZhao = new Student();
		//初始化各个属性
		stdZhao.initSelf("小赵","女",22, 78D,78D,78D);
		stdZhao.introductionSelf();
		System.out.println("我的成绩平均分是:" + stdZhao.markAvg());	
		
		
	}
}

 

四:static的作用

   如果一个成员被声明为static,它就能够在它的类的任何对象创建之前被访问,而不必引用任何对象。你可以将方法和变量都声明为static。static 成员的最常见的例子是main()。因为在程序开始执行时必须调用main() ,所以它被声明为static。

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics