1、面向对象的类定义、对象实例化、修改属性和引用对象。
1 package firstproject; 2 //定义一个car类 3 class Car 4 { 5 //描述颜色 6 String color="blue"; 7 //描述轮胎数 8 int n=4; 9 //描述行为 10 void run() 11 { 12 System.out.println("The car is running!"); 13 } 14 } 15 public class CarDemo 16 { 17 public static void main(String[] args) 18 { 19 //实例化车对象 20 Car c=new Car(); 21 //修改车的颜色属性 22 c.color="green"; 23 System.out.println(c.color); 24 c.run(); 25 Car c1=new Car(); 26 System.out.println(c1.color); 27 28 //多个引用,同一个对象 29 Car c2=new Car(); 30 Car c3=new Car(); 31 c2.color="black"; 32 c3=c2; 33 c3.n=5; 34 System.out.println(c2.n); 35 } 36 37 }
2、匿名对象当作实参。
1 package firstproject; 2 //定义一个car类 3 class Car 4 { 5 //描述颜色 6 String color="blue"; 7 //描述轮胎数 8 int n=4; 9 //描述行为 10 void run() 11 { 12 System.out.println("The car is running!"); 13 } 14 } 15 public class CarDemo 16 { 17 public static void main(String[] args) 18 { 19 //对象当作实参 20 Car c=new Car(); 21 r(c); 22 //匿名对象当作实参 23 r(new Car()); 24 } 25 public static void r(Car c) 26 { 27 c.color="red"; 28 c.n=3; 29 c.run(); 30 } 31 }
3、利用private修饰符、set函数和get函数实现封装
1 package firstproject; 3 public class People 4 { 5 public static void main(String[] args) 6 { 7 P p1=new P(); 8 //p1.age=44; 9 p1.set_Age(23); 10 System.out.println(p1.get_Age()); 11 } 12 } 13 //定义一个人的类 14 class P 15 { 16 private int age; //程序运行过程中,自始至终都没有变化过 17 public void set_Age(int age) 18 { 19 this.age=age; //左边的age是private修饰过的age 20 } 21 public int get_Age() 22 { 23 return age; 24 } 25 void speak() 26 { 27 System.out.println("I will talk !"); 28 } 29 }
4、继承、封装
1 /* 2 * 继承、多态练习 3 */ 4 public class Test 5 { 6 public static void main(String[] args) 7 { 8 //zi a=new zi(); 9 //System.out.println("a.num1="+a.num+" "+"a.num2="+a.num); 10 //a.show(); 11 //a.show1(); 12 zi b=new zi(); 13 System.out.println(b.num); 14 System.out.println(Person.MY_PAI); 15 } 16 } 17 18 //子类调用父类的构造函数 19 class fu 20 { 21 int num=8; 22 fu() 23 { 24 num=43; 25 System.out.println("fei yada"); 26 } 27 fu(int x) 28 { 29 System.out.println("fei yada"); 30 } 31 } 32 class zi extends fu 33 { 34 zi() 35 { 36 super(); //默认调用父类的构造函数 37 //super(4); 38 System.out.println("lang wei"); 39 } 40 } 41 42 //子类调用父类的函数和成员变量 43 class fu1 44 { 45 int num=23; 46 public void show() 47 { 48 System.out.println(num); 49 } 50 } 51 class zi1 extends fu1 52 { 53 54 int num=32; 55 public void show() //覆盖重写父类功能 56 { 57 //super.show(); //调用父类的方法 58 System.out.println(this.num); 59 System.out.println(super.num); 60 61 } 62 } 63 /* 64 * 子类调用父类的构造方法和一般方法的方式 65 */ 66 class Person 67 { 68 public static final double MY_PAI=3.141592653; //最终成员变量不允许二次赋值,它是一个常量 69 private String name; 70 Person(String name) 71 { 72 this.name=name; 73 } 74 void run(){} 75 } 76 class Student extends Person 77 { 78 String name; 79 Student(String name) 80 { 81 super(name); 82 } 83 void run() 84 { 85 super.run(); 86 } 87 }
5、构造函数,一般函数、属性构造器、构造代码快和this关键字的使用
1 package firstproject; 2 public class People 3 { 4 public static void main(String[] args) 5 { 6 P p1=new P(); //构造函数,只被对象调用一次 7 P p2=new P("Joe Shine",23); 8 System.out.println("之前的姓名是: "+p2.get_Name()); 9 p2.set_Name("Joe"); 10 System.out.println("之后的名字是: "+p2.get_Name()); 11 P p3=new P(23); 12 P.cry(); //一般函数,可以被对象调用多次 13 P p4=new P("xiaowang"); //实例化两个人,比较两个人的名字是否相同 14 P p5=new P("daming"); 15 boolean b=p4.compare(p5); 16 System.out.println(b); 17 } 18 } 19 //定义一个人的类 20 class P 21 { 22 private String name; 23 private int age; 24 25 public void set_Name(String name) //属性构造器 26 { 27 this.name=name; 28 } 29 public String get_Name() 30 { 31 return name; 32 } 33 34 public void set_Age(int age) //属性构造器 35 { 36 this.age=age; 37 } 38 public int get_Age() 39 { 40 return age; 41 } 42 //定义多种构造函数 43 P() 44 { 45 System.out.println("name="+name+",age="+age); 46 } 47 P(String name) 48 { 49 this.name=name; 50 } 51 P(String name,int age) 52 { 53 this(name); //this用在构造函数间调用 54 this.age=age; 55 System.out.println("name="+name+",age="+age); 56 } 57 P(int age) 58 { 59 this.age=age; 60 System.out.println("age="+age); 61 } 62 static void cry() 63 { 64 System.out.println("cry...."); 65 } 66 //构造代码块 67 { 68 System.out.println("优先执行!!!"); 69 } 70 //设计函数,比较两个人的人名是否相同,使用this关键字调用对象 71 public boolean compare(P p) 72 { 73 return this.name==p.name; 74 } 75 }
6、抽象、继承特性练习
1 /* 2 * 抽象、继承练习 3 */ 4 class BaseStudent extends Student 5 { 6 void study() 7 { 8 System.out.println("Base study"); 9 } 10 void learn(){} //子类中必须实现父抽象类中的所有方法 11 } 12 class AdvanStudent extends Student 13 { 14 void study() 15 { 16 System.out.println("Advance study"); 17 } 18 void learn(){} 19 } 20 abstract class Student //抽象方法只能在抽象类中 21 { 22 abstract void study(); 23 abstract void learn(); 24 } 25 26 /* 实例: 27 * 假如我们开发一个系统是需要对员工进行建模,员工包括三个属性:姓名、工资和工号。经理也是员工,除了具有员工的属性外, 28 * 还具有奖金属性。请使用继承的思想设计出员工类和经理类。要求类中提供必要的方法进行属性访问。 29 */ 30 31 //主函数 32 public class Test 33 { 34 public static void main(String[] args) 35 { 36 //BaseStudent stu1=new BaseStudent(); 37 //stu1.study(); 38 Manager m1=new Manager("zhangsan","092238984",5000,400); 39 Employee e1=new Employee("lisi","0123454",500); 40 System.out.println(m1.getName()); 41 System.out.println(e1.getName()); 42 m1.work(); 43 e1.work(); 44 } 45 } 46 //定义打工者,它是一个抽象类 47 abstract class Staff 48 { 49 //声明私有的成员变量 50 private String name; 51 private String id; 52 private double pay; 53 //抽象类内部可以有构造方法 54 Staff(String name,String id,double pay) 55 { 56 this.name=name; 57 this.id=id; 58 this.pay=pay; 59 } 60 //属性构造器 61 public void setName(String name) 62 { 63 this.name=name; 64 } 65 public String getName() 66 { 67 return name; 68 } 69 public void setId(String id) 70 { 71 this.id=id; 72 } 73 public String getId() 74 { 75 return id; 76 } 77 public void setPay(double pay) 78 { 79 this.pay=pay; 80 } 81 public double getPay() 82 { 83 return pay; 84 } 85 86 //定义抽象的成员方法 87 abstract void work(); 88 } 89 //员工类 90 class Employee extends Staff 91 { 92 Employee(String name,String id,double pay) 93 { 94 super(name, id, pay); 95 } 96 void work() 97 { 98 System.out.println("员工工作内容"); 99 } 100 } 101 //经理类 102 class Manager extends Staff 103 { 104 private int bonus; 105 Manager(String name,String id,double pay,int bonus) 106 { 107 super(name,id,pay); 108 this.bonus=bonus; 109 } 110 public int getBonus() 111 { 112 return bonus; 113 } 114 public void setBonus(int bonus) 115 { 116 this.bonus = bonus; 117 } 118 void work() 119 { 120 System.out.println("经理工作内容"); 121 } 122 }
7、模版方法设计模式例子
1 /* 2 * 实例:设计程序得出程序运行的时间 3 * 思路:利用System.currentTimeMillis();函数 4 */ 5 public class Test extends Code 6 { 7 public static void main(String[] args) 8 { 9 Code a=new Code(); 10 a.runcode(); 11 } 12 } 13 abstract class GetTime 14 { 15 public final void getTime() //定义为最终类,确保获取时间的方法不可以复写 16 { 17 long start=System.currentTimeMillis(); 18 runcode(); 19 long end=System.currentTimeMillis(); 20 System.out.println(" 运行时间是:"+(end-start)+"毫秒"); 21 } 22 abstract void runcode(); 23 } 24 class Code extends GetTime 25 { 26 void runcode() 27 { 28 29 } 30 }
8、多态的扩展性
1 /* 2 多态性扩展性的实例 3 */ 4 public class InterfaceDemo 5 { 6 public static void main(String[] args) 7 { 8 /* 9 Cat c=new Cat(); 10 c.eat(); 11 Dog d=new Dog(); 12 d.eat(); 13 Pig p=new Pig(); 14 p.eat(); 15 */ 16 /* 17 chi(new Dog()); 18 chi(new Pig()); 19 chi(new Cat()); 20 Cat c=new Cat(); 21 chi(c); 22 System.out.println(); 23 */ 24 //多态性概念 25 Animal c1=new Cat(); 26 c1.eat(); 27 Animal d1=new Dog(); 28 d1.eat(); 29 Animal p1=new Pig(); 30 p1.eat(); 31 System.out.println(); 32 //多态性的扩展性表现 33 chi(new Cat()); 34 chi(new Dog()); 35 chi(new Pig()); 36 37 } 38 //体现封装性的函数 39 public static void chi(Cat c) 40 { 41 c.eat(); 42 } 43 public static void chi(Dog d) 44 { 45 d.eat(); 46 } 47 public static void chi(Pig p) 48 { 49 p.eat(); 50 } 51 //表现多态性的函数 52 public static void chi(Animal a) 53 { 54 a.eat(); 55 } 56 } 57 /* 58 定义一个父类:动物 59 子类:猫,狗,猪 60 */ 61 abstract class Animal 62 { 63 public abstract void eat(); 64 } 65 class Cat extends Animal 66 { 67 public void eat() 68 { 69 System.out.println("吃鱼"); 70 } 71 public void catchMouse() 72 { 73 System.out.println("抓老鼠"); 74 } 75 } 76 class Dog extends Animal 77 { 78 public void eat() 79 { 80 System.out.println("吃骨头"); 81 } 82 public void kanjia() 83 { 84 System.out.println("看家"); 85 } 86 } 87 class Pig extends Animal 88 { 89 public void eat() 90 { 91 System.out.println("吃草"); 92 } 93 }
9、多态的转型
1 /* 2 多态性转型的实例 3 */ 4 public class InterfaceDemo 5 { 6 public static void main(String[] args) 7 { 8 //多态性概念 9 /* 10 Animal c=new Cat(); //类型提升,向上转型 11 c.eat(); 12 Cat c1=(Cat)c; //强制向下转型,否则无法使用特定方法 13 c1.catchMouse(); 14 */ 15 chi(new Cat()); 16 chi(new Dog()); 17 chi(new Pig()); 18 } 19 //表现多态性的函数 20 public static void chi(Animal a) //子类型有限用此方法 21 { 22 a.eat(); //吃的食物的类型 23 if(a instanceof Cat) 24 { 25 Cat c=(Cat)a; //将a对象的从父类强制转型为子类Cat对象 26 c.catchMouse(); 27 } 28 else if(a instanceof Dog) 29 { 30 Dog d=(Dog)a; 31 d.kanjia(); 32 } 33 else 34 System.out.println("猪的特定类型是睡觉"); 35 } 36 } 37 /* 38 定义一个父类:动物 39 子类:猫,狗,猪 40 */ 41 abstract class Animal 42 { 43 public abstract void eat(); 44 } 45 class Cat extends Animal 46 { 47 public void eat() 48 { 49 System.out.println("吃鱼"); 50 } 51 public void catchMouse() 52 { 53 System.out.println("抓老鼠"); 54 } 55 } 56 class Dog extends Animal 57 { 58 public void eat() 59 { 60 System.out.println("吃骨头"); 61 } 62 public void kanjia() 63 { 64 System.out.println("看家"); 65 } 66 } 67 class Pig extends Animal 68 { 69 public void eat() 70 { 71 System.out.println("吃草"); 72 } 73 public void sleep() 74 { 75 System.out.println("睡觉"); 76 } 77 }
10、多态扩展性例子练习
1 /* 2 多态性练习:对一个培训班的基础班,提高班,冲刺班的所有学员面向对象编程,学员共有的方法是吃饭和睡觉,睡觉需要重写。 3 思路:1、定义一个抽象的学生类; 4 2、利用多态性将多个班的学生方法抽象出来,对外提供所有学生共有特性的的访问接口,通过定义方法类来实现; 5 3、分别定义学生类的子类:基础班学生,提高班学生,冲刺班学生; 6 4、调用方法类,实例化学员对象。 7 */ 8 public class InterfaceDemo 9 { 10 public static void main(String[] args) 11 { 12 FunStudent f=new FunStudent(); 13 f.fun(new BaseStudent()); 14 f.fun(new AdvStudent()); 15 f.fun(new SprintStudent()); 16 } 17 18 } 19 //定义抽象的父类 20 abstract class Student 21 { 22 public abstract void study(); //子类必须实现的方法 23 public void sleep() 24 { 25 System.out.println("躺着睡"); 26 } 27 } 28 //定义方法类 29 class FunStudent 30 { 31 public void fun(Student s) 32 { 33 s.study(); 34 s.sleep(); 35 } 36 } 37 //定义子类 38 class BaseStudent extends Student 39 { 40 public void study() 41 { 42 System.out.println("base study"); 43 } 44 public void sleep() 45 { 46 System.out.println("站着睡"); 47 } 48 } 49 class AdvStudent extends Student 50 { 51 public void study() 52 { 53 System.out.println("Adv study"); 54 } 55 } 56 class SprintStudent extends Student 57 { 58 public void study() 59 { 60 System.out.println("sprint study"); 61 } 62 }
11、接口多态的逻辑练习
1 /* 2 接口多态的逻辑练习:电脑主板,接口,各种外插卡 3 */ 4 public class Test 5 { 6 public static void main(String[] args) 7 { 8 MainBoard mb=new MainBoard(); 9 mb.run(); 10 mb.P(new NetCard()); 11 mb.P(new SoundCard()); 12 mb.P(null); //主板没有插任何东西 13 } 14 15 } 16 //定义一个主板类 17 class MainBoard 18 { 19 public void run() 20 { 21 System.out.println("主板在运行"); 22 } 23 //接口的多态性 24 public void P(PCI p) 25 { 26 if(p!=null) 27 { 28 p.open(); 29 p.close(); 30 } 31 } 32 } 33 //定义一个接口 34 interface PCI 35 { 36 public abstract void open(); 37 public abstract void close(); 38 } 39 //定义一个网卡,执行接口 40 class NetCard implements PCI 41 { 42 public void open() 43 { 44 System.out.println("NetCard open"); 45 } 46 public void close() 47 { 48 System.out.println("NetCard close"); 49 } 50 } 51 //定义一个声卡,执行接口 52 class SoundCard implements PCI 53 { 54 public void open() 55 { 56 System.out.println("SoundCard open"); 57 } 58 public void close() 59 { 60 System.out.println("SoundCard close"); 61 } 62 }
12、Object 练习
1 /* 2 Object类练习 3 */ 4 public class Test 5 { 6 public static void main(String[] args) 7 { 8 Demo d=new Demo(4); 9 Demo d1=new Demo(5); 10 Person p=new Person(); 11 System.out.println(d.toString()); 12 System.out.println(Integer.toHexString(d.hashCode())); 13 System.out.println(p.toString()); 14 Class a=p.getClass(); 15 System.out.println(a.getName()); 16 } 17 } 18 class Person 19 {} 20 class Demo 21 { 22 private int num; 23 Demo(int x) 24 { 25 this.num=x; 26 } 27 //对象比较功能在父类Object中存在,所以就重写父类方法即可 28 public boolean equals(Object obj) //Object obj=new Demo(); 29 { 30 Demo o=(Demo)obj; 31 return this.num==o.num; 32 } 33 }
13、内部类练习1
1 /* 2 内部类练习1 3 */ 4 public class Test 5 { 6 public static void main(String[] args) 7 { 8 Outer o=new Outer(); 9 //Outer.Inner i=new Outer().new Inner(); 10 //new Outer.Inner().function(); //访问内部静态类方法 11 Outer.Inner.function(); 12 o.method(); 13 //i.function(); 14 //Inner i=new Inner(); //错误 15 System.out.println(); 16 } 17 } 18 class People 19 {} 20 21 class Outer //外部类 22 { 23 private static int a=4; 24 static class Inner //内部类 25 { 26 static void function() 27 { 28 System.out.println("我是内部类"); 29 System.out.println(a); 30 } 31 } 32 void method() 33 { 34 Inner.function(); 35 a=32; 36 //Inner i=new Inner(); 37 //i.function(); 38 System.out.println("我是外部类的方法"); 39 } 40 }
14、内部类练习2
1 /* 2 内部类练习2 3 */ 4 public class Test 5 { 6 public static void main(String[] args) 7 { 8 //new Outer().method(); //实例化内部类才可以使用此语句 9 Outer.method(); 10 } 11 } 12 class Outer //外部类 13 { 14 private static int a=2; 15 static void method() 16 { 17 final int b=4; 18 final class Inner //内部类没有定义在成员变量的位置上,不能使用成员变量的修饰符 19 { 20 void function() 21 { 22 System.out.println(a); 23 System.out.println(b); 24 } 25 } 26 new Inner().function(); //需要实例化内部类 27 } 28 }
15、匿名内部类练习1
1 /* 2 匿名内部类练习1 3 */ 4 public class Test 5 { 6 public static void main(String[] args) 7 { 8 new Outer().method(); 9 } 10 } 11 //定义一个抽象类 12 abstract class ab 13 { 14 abstract void function(); 15 } 16 class Outer //外部类 17 { 18 int a=2; 19 /* 20 class Inner extends ab //内部类继承自一个外部的抽象类 21 { 22 void function() 23 { 24 System.out.println(a); 25 } 26 } 27 */ 28 void method() 29 { 30 //new Inner().function(); 31 //匿名类对象的的建立,此对象等价于注释部分的内容 32 /* 33 new ab() 34 { 35 void function() 36 { 37 System.out.println(a); 38 } 39 void show() 40 { 41 System.out.println("我是匿名对象"); 42 } 43 }.function(); 44 new ab() 45 { 46 void function() 47 { 48 System.out.println(a); 49 } 50 void show() 51 { 52 System.out.println("我是匿名对象"); 53 } 54 }.show(); 55 */ 56 ab x=new ab() 57 { 58 void function() 59 { 60 System.out.println(a); 61 } 62 void show() 63 { 64 System.out.println("我是匿名对象"); 65 } 66 }; 67 //x.show(); //编译失败 68 x.function(); 69 } 70 }
16、匿名内部类练习2
1 /* 2 匿名内部类练习2 3 */ 4 public class Test1 5 { 6 public static void main(String[] args) 7 { 8 Test.function().method(); //给定的条件 9 //在没有父类和接口的情况下,运行一个function方法 10 new Object() //一个Object类的实例对象 11 { 12 void function() //给实例对象添加内容 13 { 14 System.out.println("我是一个奇葩"); 15 } 16 }.function(); 17 } 18 } 19 interface Inter 20 { 21 public abstract void method(); 22 } 23 24 class Test 25 { 26 //补足代码,通过匿名内部类 27 /* 28 static class Inner implements Inter 29 { 30 public void method() 31 { 32 System.out.println("我是内部类"); 33 } 34 } 35 */ 36 static Inter function() 37 { 38 //return new Inner(); 39 return new Inter() 40 { 41 public void method() 42 { 43 System.out.println("我是内部类"); 44 } 45 }; 46 } 47 }
17、自定义异常
1 /* 2 自定义异常:建立一个除法类,因为Java系统中没有负数的异常定义,所以需要自定义负数异常。 3 */ 4 public class Test1 5 { 6 public static void main(String[] args) 7 { 8 Demo d=new Demo(); 9 try 10 { 11 d.div(5, -3); 12 } 13 catch(FuShuException e) 14 { 15 System.out.print(e.toString()); 16 //System.out.println(e.getMessage()); 17 System.out.println(e.fuShu()); 18 } 19 System.out.println(); 20 } 21 } 22 class Demo 23 { 24 int div(int x,int y) throws FuShuException //可能出现问题,抛出负数异常 25 { 26 if(y<0) 27 throw new FuShuException("算数中出现了负数,不满足条件!负数是:",y); //抛出一个负数异常对象 28 return x/y; 29 } 30 } 31 //自定义异常类 32 class FuShuException extends Exception //自定义异常类继承自Exceptionl类 33 { 34 private int a; //负数值 35 FuShuException(String msg,int a) 36 { 37 super(msg); 38 this.a=a; 39 } 40 int fuShu() 41 { 42 return a; 43 } 44 45 }
18、异常的练习
1 /* 2 通过异常处理写出一个老师使用电脑授课,电脑出现异常对老师授课产生影响的程序 3 */ 4 public class ExceptionTest 5 { 6 public static void main(String[] args) 7 { 8 Teacher t=new Teacher("李老师"); 9 try 10 { 11 t.prelect(); 12 } 13 catch(StopPrelect e) 14 { 15 System.out.println(e.toString()+"开始做练习"); 16 } 17 } 18 } 19 //定义一个老师类 20 class Teacher 21 { 22 String msg; 23 private Computer com; 24 Teacher(String msg) 25 { 26 this.msg=msg; 27 com=new Computer(); //com指向Computer类的引用初始化成功,就说明电脑可以正常使用 28 } 29 public void prelect() throws StopPrelect 30 { 31 try 32 { 33 com.run(); 34 } 35 catch(LanPingException e) //解决蓝屏异常 36 { 37 System.out.println(e.toString()); 38 com.reset(); 39 System.out.println("老师修复了蓝屏异常"+" 正常上课"); 40 } 41 catch(MaoYanException e) //无法解决冒烟异常,再次抛出 42 { 43 System.out.println(e.toString()+" 老师无法修复电脑"); 44 throw new StopPrelect("停止上课"); 45 } 46 } 47 } 48 //定义一个电脑类 49 class Computer 50 { 51 private String state="运行错1"; //定义一个电脑的状态变量,正常运行 52 public String stateArgument() 53 { 54 return state; 55 } 56 public void run() throws LanPingException,MaoYanException //声明两个异常,需要去解决 57 { 58 if(state=="正常运行") 59 System.out.println("电脑正常运行"); 60 else if(state=="运行错误1") 61 throw new LanPingException("出现了蓝屏异常"); //抛出一个蓝屏异常对象 62 else if(state=="运行错误2") 63 throw new MaoYanException("出现了冒烟异常"); //抛出一个冒烟异常 64 else 65 System.out.println("电脑有其它运行错误"+" 停止授课"); 66 } 67 public void reset() 68 { 69 System.out.println("电脑在重启"); 70 } 71 } 72 //定义一个蓝屏异常 73 class LanPingException extends Exception 74 { 75 //private String message; 76 static final long serialVersionUID=1L; 77 LanPingException(String message) 78 { 79 super(message); 80 } 81 } 82 //定义一个冒烟异常 83 class MaoYanException extends Exception 84 { 85 //private String message; 86 static final long serialVersionUID=2L; 87 MaoYanException(String message) 88 { 89 super(message); 90 } 91 } 92 //定义一个无法授课的异常 93 class StopPrelect extends Exception 94 { 95 //private String message; 96 static final long serialVersionUID=3L; 97 StopPrelect(String message) 98 { 99 super(message); 100 } 101 }
19、
说明:代码基本上是从毕向东老师那里学来的。