方法的重载
构造方法重载
对象的创建和使用
class Point { private double x; private double y; Point(double x1, double y1) { x = x1; y = y1; } public double getX() { return x; } public double getY() { return y; } public void setX(double i) { x = i; } public void setY(double i) { y = i; } } class Circle { private Point o; private double radius; Circle(Point p, double r) { o = p; radius = r; } Circle(double r) { o = new Point(0.0, 0.0); radius = r; } boolean contains(Point p) { double x = p.getX() - o.getX(); double y = p.getY() - o.getY(); if(x*x + y*y > radius * radius) return false; else return true; } public void setO(double x, double y) { o.setX(x); o.setY(y); } public Point getO() { return o; } public double getRadius() { return radius;} public void setRadius(double r) { radius = r;} public double area() { return 3.14 * radius * radius; } } public class TestCircle { public static void main(String args[]) { Circle c1 = new Circle(new Point(1.0,2.0), 2.0); Circle c2 = new Circle(5.0); System.out.println("c1:("+c1.getO().getX()+"," +c1.getO().getY()+"),"+c1.getRadius()); System.out.println("c2:("+c2.getO().getX() +","+c2.getO().getY()+"),"+c2.getRadius()); System.out.println("c1 area = "+c1.area()); System.out.println("c1 area = "+c2.area()); c1.setO(5,6); c2.setRadius(9.0); System.out.println("c1:("+c1.getO().getX()+"," +c1.getO().getY()+"),"+c1.getRadius()); System.out.println("c2:("+c2.getO().getX()+"," +c2.getO().getY()+"),"+c2.getRadius()); System.out.println("c1 area = "+c1.area()); System.out.println("c1 area = "+c2.area()); Point p1 = new Point(5.2, 6.3); System.out.println(c1.contains(p1)); System.out.println(c1.contains(new Point(10.0,9.0))); } }
TestCircle.java
this关键字
static关键字
public class Cat { private static int sid = 0; private String name; int id; Cat(String name) { this.name = name; id = sid++; } public void info(){ System.out.println ("My name is "+name+" No."+id); } public static void main(String arg[]){ Cat.sid = 100; Cat mimi = new Cat("mimi"); mimi.sid = 2000; Cat pipi = new Cat("pipi"); mimi.info(); pipi.info(); } }
Cat.java
package 和 import语句
J2SDK中主要的包介绍
类的继承与权限控制
class Person { private String name; private int age; public void setName(String name) { this.name=name; } public void setAge(int age) { this.age=age; } public String getName(){ return name; } public int getAge(){ return age; } } class Student extends Person { private String school; public String getSchool() { return school; } public void setSchool(String school) { this.school =school; } } public class Test { public static void main(String arg[]){ Student student = new Student(); student.setName("John"); student.setAge(18); student.setSchool("SCH"); System.out.println(student.getName()); System.out.println(student.getAge()); System.out.println(student.getSchool()); } }
TestPerson.java
访问控制
public class TestAccess { } class T { private int i = 0; int j = 0; protected int k = 0; public int m = 0; public void m() { i = 9; } } class TT { public void m() { T t = new T(); System.out.println(t.j); } }
TestAccess.java
时间: 2024-10-12 14:39:34