java对象和类学习
定义对象的类:
- 一个对象的状态(属性或特征)是指那些具有他们当前值的数据域
- 一个对象的行为是由方法定义的,调用对象的方法就是完成对象的一个动作
使用一个通用类来定义同一类型的对象。类是一个模板,一个对象是类的一个实例,可以从一个类创建多个实例
实例如下:
- public class Main
- {
- public static void main(String args[])
- {
- Circle c1 = new Circle();
- System.out.println("The area of the circle of radius " + c1.radius + " is " + c1.getArea());
- Circle c2 = new Circle(25);
- System.out.println("The area of the circle of radius " + c2.radius + " is " + c2.getArea());
- c2.radius = 100;
- System.out.println("The area of the circle of radius " + c2.radius + " is " + c2.getArea());
- }
- }
- class Circle{
- double radius;
- Circle() {
- radius = 1.0;
- }
- Circle(double newRadius) {
- radius = newRadius;
- }
- double getArea() {
- return radius * radius * Math.PI;
- }
- }
可以把两个类放在同一个文件中,但是文件中只能有一个类是公共的,除此之外,公共类必须与文件同名,如上所示。
构造函数三特性:
1、必须具备和所在类同名
2、没有返回值,连void都没有
3、构造函数在创建一个对象使用new操作符时调用,构造函数的作用是初始化对象
在创建一个对象之后,它的数据和方法可以使用圆点运算符(.)来访问和调用,即对象成员访问运算符
数据域称为实例变量,方法称为实例方法
数据域也可能是引用型的,如下面的Student类包含一个String类型的name数据域:
- class Student{
- String name;
- int age;
- boolean isScienceMajor;
- char gender;
- }
引用类型数据域的默认值是null,数值类型数据域的默认值是0,boolean类型数据域的默认值是false,char类型数据域的默认值是‘\u0000’
对上面的Student对象中数据域的默认值进行测试:
- public class Main
- {
- public static void main(String args[])
- {
- Student student = new Student();
- System.out.println("name? " + student.name); //name? null
- System.out.println("age? " + student.age); //age? 0
- System.out.println("isScienceMajor? " + student.isScienceMajor); //isScienceMajor? false
- System.out.println("gender? " + student.gender); //gender?
- }
- }
使用java库中的类
Date类:
- public class Main
- {
- public static void main(String args[])
- {
- java.util.Date date = new java.util.Date();
- System.out.println("The elapsed time since Jan 1, 1970 is " + date.getTime() + " milliseconds");
- //The elapsed time since Jan 1, 1970 is 1404740843243 milliseconds
- System.out.println(date.toString());
- //Mon Jul 07 21:47:23 GMT+08:00 2014
- }
- }
Random类
可以使用Math.random()获取一个(0.0,1.0)之间的随机double型值,另一种产生随机数的方法是使用java.util.Random类,它产生一个int、long、double、float和boolean型值
创建一个Random对象时,必须指定一个种子或者使用默认的种子,无参构造方法使用当前已经逝去的时间作为种子
- public class Main
- {
- public static void main(String args[])
- {
- Random random = new Random();
- int Irand = random.nextInt();
- int Isrand = random.nextInt(100);
- long lrand = random.nextLong();
- double drand = random.nextDouble();
- float frand = random.nextFloat();
- boolean brand = random.nextBoolean();
- System.out.println("random.nextint(): " + Irand);
- System.out.println("random.nextInt(100) " + Isrand);
- System.out.println("random.nextLong() " + lrand);
- System.out.println("random.nextDouble() " + drand );
- System.out.println("random.nextFloat() " + frand);
- System.out.println("random.nextBoolean() " + brand);
- }
- }
- /*
- random.nextint(): -678227104
- random.nextInt(100) 9
- random.nextLong() 2478178459367264996
- random.nextDouble() 0.5726788505253162
- random.nextFloat() 0.5969435
- random.nextBoolean() false
- */
静态变量、常量和方法
如果想让一个类的所有实例共享数据,就使用静态变量(或类变量)。静态变量将变量值存储在一个公共的内存地址,故所有的对象都可以修改这个变量
无需创建类实例就可以调用静态方法
- public class Main
- {
- public static void main(String args[])
- {
- System.out.println("Before creating objects");
- System.out.println("The number of Circle objects is " + Circle2.numberOfObjects); //The number of Circle objects is 0
- Circle2 c1 = new Circle2();
- System.out.println("\nAfter creating c1");
- System.out.println("c1: radius (" + c1.radius + //c1: radius (1.0) and number of Circle objects (1)
- ") and number of Circle objects (" +
- c1.numberOfObjects + ")");
- Circle2 c2 = new Circle2(5);
- c1.radius = 9;
- System.out.println("\nAfter creating c2 and modifying c1");
- System.out.println("c1: radius (" + c1.radius + //c1: radius (9.0) and number of Circle objects (2)
- ") and number of Circle objects (" +
- c1.numberOfObjects + ")");
- System.out.println("c2: radius (" + c2.radius + //c2: radius (5.0) and number of Circle objects (2)
- ") and number of Circle objects (" +
- c2.numberOfObjects + ")");
- }
- }
- class Circle2 {
- double radius;
- static int numberOfObjects = 0;
- public Circle2() {
- radius = 1.0;
- numberOfObjects++;
- }
- Circle2(double newRadius) {
- radius = newRadius;
- numberOfObjects++;
- }
- static int getNumberOfObject() {
- return numberOfObjects;
- }
- double getArea() {
- return radius * radius * Math.PI;
- }
- }
静态变量和静态方法既可以在类的实例方法中使用,也可以在类的静态方法中使用,但是,实例变量和实例方法只能在实例方法中使用,不能在静态方法中使用
- public class Main
- {
- int i = 5;
- static int k = 2;
- public static void main(String args[])
- {
- Main mm = new Main();
- int j = mm.i;
- mm.m1();
- }
- public void m1() {
- i = i + k + m2(i, k);
- }
- public static int m2(int i, int j) {
- return (int)(Math.pow(i, j));
- }
- }
包可以用来组织类,在程序中首先应该出现语句: package packagename;
如果定义类的时候没有声明包,则在默认包中
private修饰符限定方法和数据域只能在它自己的类中被访问,默认修饰符将访问权限限定在包内,而公共的修饰符没有限定权限
如果一个类没有被定义为公共的,那么它只能在同一个包内被访问
- package p1;
- public class c1 {
- public int x;
- int y;
- private int z;
- public void m1() {
- }
- void m2() {
- }
- private void m3() {
- }
- }
- package p1;
- public class c2 {
- void aMethod() {
- c1 o = new c1();
- o.x = 1;
- o.y = 2;
- o.z = 2; //error
- o.m1();
- o.m2();
- o.m3(); //error
- }
- }
- package p2;
- import com.sun.org.apache.xml.internal.security.c14n.helper.C14nHelper;
- public class c3 {
- void aMethod() {
- C1 o = new c1();
- o.x;
- o.y; //error
- o.z; //error
- o.m1();
- o.m2(); //error
- o.m3(); //error
- }
- }
修饰符private只能应用在类的成员上,在定义私有数据域的类外的对象是不能访问这个数据域的,为了能访问私有数据域,可以提供一个get方法返回数据域的值
为了能够更新一个数据域,可以提供一个set方法给数据域设置新值
- public class Circle {
- private double radius = 1;
- private static int numberOfObjects = 0;
- public Circle() {
- numberOfObjects++;
- }
- public Circle(double newRadius) {
- radius = newRadius;
- numberOfObjects++;
- }
- public double getRadius() {
- return radius;
- }
- public void setRadius(double newRadius) {
- radius = (newRadius >= 0) ? newRadius : 0;
- }
- public static int getNumberOfObjects() {
- return numberOfObjects;
- }
- public double getArea() {
- return radius * radius * Math.PI;
- }
- }
Circle类
- public class Main
- {
- public static void main(String args[])
- {
- Circle myCircle = new Circle(5.0); //creat a Circle
- System.out.println("The area of the circle of radius "
- + myCircle.getRadius() + " is " + myCircle.getArea());
- //Increase myCircle's radius
- myCircle.setRadius(myCircle.getRadius() * 1.1);
- System.out.println("The area of the circle of radius " + myCircle.getRadius() + " is "
- + myCircle.getArea());
- System.out.println("The number of objrcts created is " + Circle.getNumberOfObjects());
- }
- }
将对象传递给方法,实际上是传递对象的引用
java只有一种参数传递的方式:值传递。传递对象实际上是传递对象的引用值
- public class Main
- {
- public static void main(String args[])
- {
- Circle myCircle = new Circle(1);
- int n = 5;
- printAreas(myCircle, n);
- System.out.println("\n" + "Radius is " + myCircle.getRadius());
- System.out.println("n is " + n);
- }
- public static void printAreas(Circle c, int times) {
- System.out.println("Radius \t\tArea");
- while(times >= 0) {
- System.out.println(c.getRadius() + "\t\t" + c.getArea());
- c.setRadius(c.getRadius() + 1);
- times--;
- }
- }
- }
对象数组:对象数组实际上是引用变量的数组
- public static void main(String args[])
- {
- Circle[] circleArray = new Circle[10];
- for(int i = 0; i < circleArray.length; ++i) {
- circleArray[i] = new Circle();
- }
- }
java对象和类学习的更多相关文章
- Java-Runoob:Java 对象和类
ylbtech-Java-Runoob:Java 对象和类 1.返回顶部 1. Java 对象和类 Java作为一种面向对象语言.支持以下基本概念: 多态 继承 封装 抽象 类 对象 实例 方法 重载 ...
- (五)Java 对象和类
Java 对象和类 Java作为一种面向对象语言.支持以下基本概念: 多态 继承 封装 抽象 类 对象 实例 方法 消息解析 本节我们重点研究对象和类的概念. 对象:对象是类的一个实例,有状态和行为. ...
- Java 教程 (Java 对象和类)
Java 对象和类 Java作为一种面向对象语言.支持以下基本概念: 多态 继承 封装 抽象 类 对象 实例 方法 重载 本节我们重点研究对象和类的概念. 对象:对象是类的一个实例(对象不是找个女朋友 ...
- JavaSE基础(十二)--Java 对象和类
Java 对象和类 Java作为一种面向对象语言.支持以下基本概念: 多态 继承 封装 抽象 类 对象 实例 方法 重载 本节我们重点研究对象和类的概念. 对象:对象是类的一个实例(对象不是找个女朋友 ...
- 重新学习Java——对象和类(一)
之前通过记笔记的方法,对于<Java核心技术>这本书的前一章进行了重新的复习,感觉效果很好,比单独看书带来了更好的复习效果,了解了很多以前不是很注意的一些细节,但是在一些自己较为熟悉的地方 ...
- 重新学习Java——对象和类(二)
上一节回归了如何以面向对象的思想去使用一些Java中的公共类,也设计了一些自己的类并介绍了设计类的基本方法和技巧,这一节我们将继续回顾这些内容,并争取从中获得新的体验和感受. 1. 静态域与静态方法 ...
- java.util.Properties类 学习笔记
学习目标: 1.认识properties文件,理解其含义,会正确创建properties文件. 2.会使用java.util.Properties类来操作properties文件. 3.掌握相对路 ...
- Java对象与类中的一个小练习
一直在Eclipse里做练习.是做一个练习,执行一个的那种.刚刚学习了Java的对象与类,练习中把类和执行放在同一包下的两个.java文件里面了.是可以执行的.(Get) 相关代码: public c ...
- JAVA对象和类
Java作为一种面向对象语言.支持以下基本概念: 多态 继承 封装 抽象 类 对象 实例 方法 重载 Java中的类 类可以看成是创建Java对象的模板. 通过下面一个简单的类来理解下Java中类的定 ...
随机推荐
- 团体程序设计天梯赛-练习集L1-007. 念数字
L1-007. 念数字 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 翁恺 输入一个整数,输出每个数字对应的拼音.当整数为负数时,先 ...
- PHP运算符及php取整函数
ceil -- 进一法取整 说明 float ceil ( float value ) 返回不小于 value 的下一个整数,value 如果有小数部分则进一位.ceil() 返回的类型仍然是 flo ...
- dtp--eclipse的安装数据源管理的一个插件的安装方法
1. 下载eclipse dtp 插件 http://download.eclipse.org/datatools/updates/1.11 help——>install new softwa ...
- highcharts 柱形堆叠图
<!doctype html> <html lang="en"> <head> <script type="text/javas ...
- Spring中的实例生成方式及其生命周期
三种实例化bean的方式1.使用类构造器实例化 <!-- 使用类构造器实例化,class属性表示要使用的类的全限定名 --> <bean id="userDao1" ...
- P66、面试题8:旋转数组的最小数字
题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数 ...
- linux下关于程序性能和系统性能的工具、方法
观察性能/状态的方法:top free netstat /pro/目录下的信息 其中/pro/meminfo下的信息相当丰富 ------------------------------------- ...
- Android开源滤镜 仿instagram
前段时间做一个项目的时候发现一个不错的滤镜库,是仿Instagram效果的,能够实现Lomo在内的十几种滤镜效果,git地址是: https://github.com/beartung/insta-f ...
- Spring事务Transaction配置的五种注入方式详解
Spring事务Transaction配置的五种注入方式详解 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学 ...
- Android横竖屏切换总结
Android横竖屏切换总结(Android资料) Android横竖屏要解决的问题应该就两个: 一.布局问题 二.重新载入问题 1.布局问题:如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的 ...