标签:style blog http java color 使用
定义对象的类:
使用一个通用类来定义同一类型的对象。类是一个模板,一个对象是类的一个实例,可以从一个类创建多个实例
实例如下:
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; } }
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(); } }
标签:style blog http java color 使用
原文地址:http://www.cnblogs.com/bigjava/p/3830073.html