定义对象的类:

  • 一个对象的状态(属性或特征)是指那些具有他们当前值的数据域
  • 一个对象的行为是由方法定义的,调用对象的方法就是完成对象的一个动作

使用一个通用类来定义同一类型的对象。类是一个模板,一个对象是类的一个实例,可以从一个类创建多个实例

实例如下:

  1. public class Main
  2. {
  3. public static void main(String args[])
  4. {
  5. Circle c1 = new Circle();
  6. System.out.println("The area of the circle of radius " + c1.radius + " is " + c1.getArea());
  7.  
  8. Circle c2 = new Circle(25);
  9. System.out.println("The area of the circle of radius " + c2.radius + " is " + c2.getArea());
  10. c2.radius = 100;
  11. System.out.println("The area of the circle of radius " + c2.radius + " is " + c2.getArea());
  12.  
  13. }
  14. }
  15. class Circle{
  16. double radius;
  17. Circle() {
  18. radius = 1.0;
  19. }
  20. Circle(double newRadius) {
  21. radius = newRadius;
  22. }
  23. double getArea() {
  24. return radius * radius * Math.PI;
  25. }
  26. }

可以把两个类放在同一个文件中,但是文件中只能有一个类是公共的,除此之外,公共类必须与文件同名,如上所示。

构造函数三特性:

1、必须具备和所在类同名

2、没有返回值,连void都没有

3、构造函数在创建一个对象使用new操作符时调用,构造函数的作用是初始化对象

在创建一个对象之后,它的数据和方法可以使用圆点运算符(.)来访问和调用,即对象成员访问运算符

数据域称为实例变量,方法称为实例方法

数据域也可能是引用型的,如下面的Student类包含一个String类型的name数据域:

  1. class Student{
  2. String name;
  3. int age;
  4. boolean isScienceMajor;
  5. char gender;
  6. }

引用类型数据域的默认值是null,数值类型数据域的默认值是0,boolean类型数据域的默认值是false,char类型数据域的默认值是‘\u0000’

对上面的Student对象中数据域的默认值进行测试:

  1. public class Main
  2. {
  3. public static void main(String args[])
  4. {
  5. Student student = new Student();
  6. System.out.println("name? " + student.name); //name? null
  7. System.out.println("age? " + student.age); //age? 0
  8. System.out.println("isScienceMajor? " + student.isScienceMajor); //isScienceMajor? false
  9. System.out.println("gender? " + student.gender); //gender?
  10.  
  11. }
  12. }

使用java库中的类

Date类:

  1. public class Main
  2. {
  3. public static void main(String args[])
  4. {
  5. java.util.Date date = new java.util.Date();
  6. System.out.println("The elapsed time since Jan 1, 1970 is " + date.getTime() + " milliseconds");
  7. //The elapsed time since Jan 1, 1970 is 1404740843243 milliseconds
  8. System.out.println(date.toString());
  9. //Mon Jul 07 21:47:23 GMT+08:00 2014
  10. }
  11. }

Random类

可以使用Math.random()获取一个(0.0,1.0)之间的随机double型值,另一种产生随机数的方法是使用java.util.Random类,它产生一个int、long、double、float和boolean型值

创建一个Random对象时,必须指定一个种子或者使用默认的种子,无参构造方法使用当前已经逝去的时间作为种子

  1. public class Main
  2. {
  3. public static void main(String args[])
  4. {
  5. Random random = new Random();
  6. int Irand = random.nextInt();
  7. int Isrand = random.nextInt(100);
  8. long lrand = random.nextLong();
  9. double drand = random.nextDouble();
  10. float frand = random.nextFloat();
  11. boolean brand = random.nextBoolean();
  12. System.out.println("random.nextint(): " + Irand);
  13. System.out.println("random.nextInt(100) " + Isrand);
  14. System.out.println("random.nextLong() " + lrand);
  15. System.out.println("random.nextDouble() " + drand );
  16. System.out.println("random.nextFloat() " + frand);
  17. System.out.println("random.nextBoolean() " + brand);
  18. }
  19. }
  20. /*
  21. random.nextint(): -678227104
  22. random.nextInt(100) 9
  23. random.nextLong() 2478178459367264996
  24. random.nextDouble() 0.5726788505253162
  25. random.nextFloat() 0.5969435
  26. random.nextBoolean() false
  27. */

静态变量、常量和方法

如果想让一个类的所有实例共享数据,就使用静态变量(或类变量)。静态变量将变量值存储在一个公共的内存地址,故所有的对象都可以修改这个变量

无需创建类实例就可以调用静态方法

  1. public class Main
  2. {
  3. public static void main(String args[])
  4. {
  5. System.out.println("Before creating objects");
  6. System.out.println("The number of Circle objects is " + Circle2.numberOfObjects); //The number of Circle objects is 0
  7. Circle2 c1 = new Circle2();
  8.  
  9. System.out.println("\nAfter creating c1");
  10. System.out.println("c1: radius (" + c1.radius + //c1: radius (1.0) and number of Circle objects (1)
  11. ") and number of Circle objects (" +
  12. c1.numberOfObjects + ")");
  13. Circle2 c2 = new Circle2(5);
  14.  
  15. c1.radius = 9;
  16. System.out.println("\nAfter creating c2 and modifying c1");
  17. System.out.println("c1: radius (" + c1.radius + //c1: radius (9.0) and number of Circle objects (2)
  18. ") and number of Circle objects (" +
  19. c1.numberOfObjects + ")");
  20. System.out.println("c2: radius (" + c2.radius + //c2: radius (5.0) and number of Circle objects (2)
  21. ") and number of Circle objects (" +
  22. c2.numberOfObjects + ")");
  23. }
  24. }
  25.  
  26. class Circle2 {
  27. double radius;
  28. static int numberOfObjects = 0;
  29.  
  30. public Circle2() {
  31. radius = 1.0;
  32. numberOfObjects++;
  33. }
  34.  
  35. Circle2(double newRadius) {
  36. radius = newRadius;
  37. numberOfObjects++;
  38. }
  39.  
  40. static int getNumberOfObject() {
  41. return numberOfObjects;
  42. }
  43.  
  44. double getArea() {
  45. return radius * radius * Math.PI;
  46. }
  47. }

静态变量和静态方法既可以在类的实例方法中使用,也可以在类的静态方法中使用,但是,实例变量和实例方法只能在实例方法中使用,不能在静态方法中使用

  1. public class Main
  2. {
  3. int i = 5;
  4. static int k = 2;
  5. public static void main(String args[])
  6. {
  7. Main mm = new Main();
  8. int j = mm.i;
  9. mm.m1();
  10. }
  11. public void m1() {
  12. i = i + k + m2(i, k);
  13.  
  14. }
  15.  
  16. public static int m2(int i, int j) {
  17. return (int)(Math.pow(i, j));
  18. }
  19. }

包可以用来组织类,在程序中首先应该出现语句: package packagename;

如果定义类的时候没有声明包,则在默认包中

private修饰符限定方法和数据域只能在它自己的类中被访问,默认修饰符将访问权限限定在包内,而公共的修饰符没有限定权限

如果一个类没有被定义为公共的,那么它只能在同一个包内被访问

  1. package p1;
  2.  
  3. public class c1 {
  4. public int x;
  5. int y;
  6. private int z;
  7. public void m1() {
  8.  
  9. }
  10. void m2() {
  11.  
  12. }
  13. private void m3() {
  14.  
  15. }
  16. }
  1. package p1;
  2.  
  3. public class c2 {
  4. void aMethod() {
  5. c1 o = new c1();
  6. o.x = 1;
  7. o.y = 2;
  8. o.z = 2; //error
  9. o.m1();
  10. o.m2();
  11. o.m3(); //error
  12.  
  13. }
  14. }
  1. package p2;
  2.  
  3. import com.sun.org.apache.xml.internal.security.c14n.helper.C14nHelper;
  4.  
  5. public class c3 {
  6. void aMethod() {
  7. C1 o = new c1();
  8. o.x;
  9. o.y; //error
  10. o.z; //error
  11. o.m1();
  12. o.m2(); //error
  13. o.m3(); //error
  14.  
  15. }
  16. }

修饰符private只能应用在类的成员上,在定义私有数据域的类外的对象是不能访问这个数据域的,为了能访问私有数据域,可以提供一个get方法返回数据域的值

为了能够更新一个数据域,可以提供一个set方法给数据域设置新值

  1. public class Circle {
  2. private double radius = 1;
  3. private static int numberOfObjects = 0;
  4. public Circle() {
  5. numberOfObjects++;
  6. }
  7. public Circle(double newRadius) {
  8. radius = newRadius;
  9. numberOfObjects++;
  10. }
  11. public double getRadius() {
  12. return radius;
  13. }
  14. public void setRadius(double newRadius) {
  15. radius = (newRadius >= 0) ? newRadius : 0;
  16. }
  17. public static int getNumberOfObjects() {
  18. return numberOfObjects;
  19. }
  20. public double getArea() {
  21. return radius * radius * Math.PI;
  22. }
  23. }

Circle类

  1. public class Main
  2. {
  3. public static void main(String args[])
  4. {
  5. Circle myCircle = new Circle(5.0); //creat a Circle
  6. System.out.println("The area of the circle of radius "
  7. + myCircle.getRadius() + " is " + myCircle.getArea());
  8.  
  9. //Increase myCircle's radius
  10. myCircle.setRadius(myCircle.getRadius() * 1.1);
  11. System.out.println("The area of the circle of radius " + myCircle.getRadius() + " is "
  12. + myCircle.getArea());
  13.  
  14. System.out.println("The number of objrcts created is " + Circle.getNumberOfObjects());
  15. }
  16. }

将对象传递给方法,实际上是传递对象的引用

java只有一种参数传递的方式:值传递。传递对象实际上是传递对象的引用值

  1. public class Main
  2. {
  3. public static void main(String args[])
  4. {
  5. Circle myCircle = new Circle(1);
  6. int n = 5;
  7. printAreas(myCircle, n);
  8. System.out.println("\n" + "Radius is " + myCircle.getRadius());
  9. System.out.println("n is " + n);
  10. }
  11.  
  12. public static void printAreas(Circle c, int times) {
  13. System.out.println("Radius \t\tArea");
  14. while(times >= 0) {
  15. System.out.println(c.getRadius() + "\t\t" + c.getArea());
  16. c.setRadius(c.getRadius() + 1);
  17. times--;
  18. }
  19. }
  20. }

 

对象数组:对象数组实际上是引用变量的数组

  1. public static void main(String args[])
  2. {
  3. Circle[] circleArray = new Circle[10];
  4. for(int i = 0; i < circleArray.length; ++i) {
  5. circleArray[i] = new Circle();
  6. }
  7. }

 

java对象和类学习的更多相关文章

  1. Java-Runoob:Java 对象和类

    ylbtech-Java-Runoob:Java 对象和类 1.返回顶部 1. Java 对象和类 Java作为一种面向对象语言.支持以下基本概念: 多态 继承 封装 抽象 类 对象 实例 方法 重载 ...

  2. (五)Java 对象和类

    Java 对象和类 Java作为一种面向对象语言.支持以下基本概念: 多态 继承 封装 抽象 类 对象 实例 方法 消息解析 本节我们重点研究对象和类的概念. 对象:对象是类的一个实例,有状态和行为. ...

  3. Java 教程 (Java 对象和类)

    Java 对象和类 Java作为一种面向对象语言.支持以下基本概念: 多态 继承 封装 抽象 类 对象 实例 方法 重载 本节我们重点研究对象和类的概念. 对象:对象是类的一个实例(对象不是找个女朋友 ...

  4. JavaSE基础(十二)--Java 对象和类

    Java 对象和类 Java作为一种面向对象语言.支持以下基本概念: 多态 继承 封装 抽象 类 对象 实例 方法 重载 本节我们重点研究对象和类的概念. 对象:对象是类的一个实例(对象不是找个女朋友 ...

  5. 重新学习Java——对象和类(一)

    之前通过记笔记的方法,对于<Java核心技术>这本书的前一章进行了重新的复习,感觉效果很好,比单独看书带来了更好的复习效果,了解了很多以前不是很注意的一些细节,但是在一些自己较为熟悉的地方 ...

  6. 重新学习Java——对象和类(二)

    上一节回归了如何以面向对象的思想去使用一些Java中的公共类,也设计了一些自己的类并介绍了设计类的基本方法和技巧,这一节我们将继续回顾这些内容,并争取从中获得新的体验和感受. 1. 静态域与静态方法 ...

  7. java.util.Properties类 学习笔记

    学习目标:   1.认识properties文件,理解其含义,会正确创建properties文件. 2.会使用java.util.Properties类来操作properties文件. 3.掌握相对路 ...

  8. Java对象与类中的一个小练习

    一直在Eclipse里做练习.是做一个练习,执行一个的那种.刚刚学习了Java的对象与类,练习中把类和执行放在同一包下的两个.java文件里面了.是可以执行的.(Get) 相关代码: public c ...

  9. JAVA对象和类

    Java作为一种面向对象语言.支持以下基本概念: 多态 继承 封装 抽象 类 对象 实例 方法 重载 Java中的类 类可以看成是创建Java对象的模板. 通过下面一个简单的类来理解下Java中类的定 ...

随机推荐

  1. 团体程序设计天梯赛-练习集L1-007. 念数字

    L1-007. 念数字 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 翁恺 输入一个整数,输出每个数字对应的拼音.当整数为负数时,先 ...

  2. PHP运算符及php取整函数

    ceil -- 进一法取整 说明 float ceil ( float value ) 返回不小于 value 的下一个整数,value 如果有小数部分则进一位.ceil() 返回的类型仍然是 flo ...

  3. dtp--eclipse的安装数据源管理的一个插件的安装方法

    1.  下载eclipse dtp 插件 http://download.eclipse.org/datatools/updates/1.11 help——>install new softwa ...

  4. highcharts 柱形堆叠图

    <!doctype html> <html lang="en"> <head> <script type="text/javas ...

  5. Spring中的实例生成方式及其生命周期

    三种实例化bean的方式1.使用类构造器实例化 <!-- 使用类构造器实例化,class属性表示要使用的类的全限定名 --> <bean id="userDao1" ...

  6. P66、面试题8:旋转数组的最小数字

    题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数 ...

  7. linux下关于程序性能和系统性能的工具、方法

    观察性能/状态的方法:top free netstat /pro/目录下的信息 其中/pro/meminfo下的信息相当丰富 ------------------------------------- ...

  8. Android开源滤镜 仿instagram

    前段时间做一个项目的时候发现一个不错的滤镜库,是仿Instagram效果的,能够实现Lomo在内的十几种滤镜效果,git地址是: https://github.com/beartung/insta-f ...

  9. Spring事务Transaction配置的五种注入方式详解

    Spring事务Transaction配置的五种注入方式详解 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学 ...

  10. Android横竖屏切换总结

    Android横竖屏切换总结(Android资料) Android横竖屏要解决的问题应该就两个: 一.布局问题 二.重新载入问题 1.布局问题:如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的 ...