类:
- public class lianxi {
- double length;
- double width;
- double area()
- {
- return length*width;
- }
- }
对象:
- public class lianxi {
- double length;
- double width;
- double area()
- {
- return length*width;
- }
- public static void main(String[] args) {
- lianxi lx=new lianxi();
- System.out.print(lx.area());
- }
- }
封装(成员访问):
- /**
- * 这是封装的类
- */
- package Bean;
- public class Rectagle {
- private double length;
- private double width;
- double area() {
- return length * width;
- }
- public void display()
- {
- System.out.println("长"+length+"\t 宽"+width);
- System.out.println("长方形面积是"+area());
- }
- public double getLength() {
- return length;
- }
- public void setLength(double length) {
- this.length = length;
- }
- public double getWidth() {
- return width;
- }
- public void setWidth(double width) {
- this.width = width;
- }
- }
- /**
- * 这是调用封装的类
- */
- import Bean.Rectagle;
- public class lianxi {
- public static void main(String args[]) {
- Rectagle r = new Rectagle();
- r.setLength(10);
- r.setWidth(5);
- r.display();
- }
- }
封装(构造方法):
- /**
- * 这是封装的类
- */
- package Bean;
- public class Rectagle {
- private double length;
- private double width;
- public Rectagle(double length, double width) {
- // TODO Auto-generated constructor stub
- this.length = length;//this关键字表示当前对象
- this.width = width;
- }
- public double area() {
- return this.length * this.width;
- }
- public double getLength() {
- return length;
- }
- public void setLength(double length) {
- this.length = length;
- }
- public double getWidth() {
- return width;
- }
- public void setWidth(double width) {
- this.width = width;
- }
- }
- /**
- * 这是调用封装的类
- */
- import Bean.Rectagle;
- public class lianxi {
- public static void main(String args[]) {
- Rectagle rectagle = new Rectagle(10.0, 5.0);//调用带参构造方法
- System.out.println("length="+rectagle.getLength()+" width="+rectagle.getWidth());
- System.out.println("area="+rectagle.area());
- }
- }
继承(派生):
- class People {
- String name;
- int age;
- String id = "工人";
- public People(String name, int age) {// 构造方法
- this.name = name;
- this.age = age;
- }
- public void talk() {
- System.out.println("能说话");
- }
- public void work() {
- System.out.println("能工作");
- }
- }
- /**
- * 学生类,派生自people类
- */
- class Student extends People {
- String sNo;
- String id = "学生";
- public Student(String name, int age, String sNo) {
- super(name, age);
- this.sNo = sNo;
- }
- }
- /**
- * 教师类,派生自people类
- *
- * @author Administrator
- *
- */
- class Teather extends People {
- String sNo;
- String id = "教师";
- public Teather(String name, int age, String sNo) {
- super(name, age);
- this.sNo = sNo;
- }
- }
- public class Test {
- public static void main(String[] args) {
- Student student=new Student("张三", 12, "20090221");
- System.out.println("name"+student.name+" age"+student.age+" sNo"+student.sNo+" id"+student.id);
- student.talk();
- student.work();
- Teather teather=new Teather("lee", 32, "20090221");
- System.out.println("name"+teather.name+" age"+teather.age+" sNo"+teather.sNo+" id"+teather.id);
- teather.talk();
- teather.work();
- }
- }
继承:
- class A {
- int x, y;
- int z = 100;
- public A(int x, int y) {// 构造方法
- this.x = x;
- this.y = y;
- }
- public void display() {
- System.out.println("在A里x=" + x + " y=" + y);
- }
- }
- /**
- * 子类B继承父类
- *
- * @author Administrator
- *
- */
- class B extends A {
- int a, b;
- int z = 200;// 在子类中定义与父类同名的变量z
- public B(int x, int y, int a, int b) {// 构造方法
- super(x, y);// 调用父类的构造方法
- // TODO Auto-generated constructor stub
- this.a = a;
- this.b = b;
- }
- public void display() {
- super.display();// 重写父类的方法
- System.out.println("在B里 a=" + a + " b=" + b);
- System.out.println("子类的变量" + z);
- System.out.println("父类变量" + super.z);
- }
- }
- public class SuperDemo {
- public static void main(String[] args) {
- B a = new B(1, 2, 3, 4);
- a.display();
- }
- }
多态:
- import org.omg.CORBA.PUBLIC_MEMBER;
- class People {
- String name;
- int age;
- String id = "工人";
- // 重载
- public People()// 无参构造方法
- {
- name = "";
- age = 0;
- }
- public People(String name, int age) {// 构造方法
- this.name = name;
- this.age = age;
- }
- public void talk() {
- System.out.println("能说话");
- }
- public void work() {
- System.out.println("能工作");
- }
- }
- /**
- * 学生类,派生自people类
- */
- class Student extends People {
- String sNo;
- String id = "学生";
- public Student(String name, int age, String sNo) {
- super(name, age);
- this.sNo = sNo;
- }
- //重写父类方法
- public void talk() {
- System.out.println("学生能回答问题");
- }
- public void work() {
- System.out.println("学生的工作是上课");
- }
- }
- /**
- * 教师类,派生自people类
- *
- * @author Administrator
- *
- */
- class Teather extends People {
- String sNo;
- String id = "教师";
- public Teather(String name, int age, String sNo) {
- super(name, age);
- this.sNo = sNo;
- }
- //重写父类方法
- public void talk() {
- System.out.println("教师能讲课");
- }
- public void work() {
- System.out.println("教师的工作是教书");
- }
- }
- public class Test {
- public static void main(String[] args) {
- Student student = new Student("张三", 12, "20090221");
- System.out.println("name" + student.name + " age" + student.age
- + " sNo" + student.sNo + " id" + student.id);
- student.talk();
- student.work();
- Teather teather = new Teather("lee", 32, "20090221");
- System.out.println("name" + teather.name + " age" + teather.age
- + " sNo" + teather.sNo + " id" + teather.id);
- teather.talk();
- teather.work();
- }
- }