public class Ex 613 public static void mainString
实例 一:重载的使用(续) public class Ex 6_13{ public static void main(String args[]) { Example_Overloading_Add add=new Example_Overloading_Add(); System. out. println("调用两个整型加法的重载方法"); System. out. println("结果是:"+add. add(3, 100)); System. out. println("调用三个双精度型加法的重载方法"); System. out. println("结果是: 运行结果: "+add. add(1. 0, 23. 987, 39. 369)); 调用两个整型加法的重载方法 结果是: 103 System. out. println("调用两个双精度型加法的重载方法"); 调用三个双精度型加法的重载方法 System. out. println("结果是:"+add. add(59. 12, 79. 123)); 结果是: 64. 356 } } 调用两个双精度型加法的重载方法 结果是: 138. 243
实例二:多态的设计(续) import java. util. Random; 类Animal的 class Animal { //动物类 sound()方法体为空 protected String type; //种类 protected String name; //名称 protected String breed; //品种 public Animal(String type, String name, String breed) { this. type=new String(type); this. name=new String(name); this. breed=new String(breed); } public String to. String() { return "This is a "+type+"n. It's "+name+" the "+breed; } public void sound() {} }
实例二:多态的设计(续) class Dog extends Animal { //Dog类 public Dog(String name) { super("Dog", name, "Unknow"); } public Dog(String name, String breed) { super("Dog", name, breed); } public void sound() { System. out. println("Woof"); } } class Cat extends Animal { //Cat类 public Cat(String name) { super("Cat", name, "Unknow"); } public Cat(String name, String breed) { super("Cat", name, breed); } public void sound() { System. out. println("Miiaooww"); } } class Duck extends Animal { //Duck类 public Duck(String name) { super("Duck", name, "Unknow"); } public Duck(String name, String breed) { super("Duck", name, breed); } public void sound() { System. out. println("Quack quack"); } } 通过Animal类派生得到的Dog、Cat、Duck类都重新 定义了sound()方法,覆盖了父类的sound()方法
实例二:多态的设计(续) public class Ex 6_14 { //测试类 运行结果: public static void main(String[] args) { Animal[] the. Animals={ //创建对象数组 Your Choice: This is a Dog new Dog("Rover", "Poodle"), It's Rover the Poodle new Cat("Max", "Abyssinian"), Woof new Duck("Daffy", "Aylesbury")}; Animal pet. Choice; //声明父类的引用 Random select=new Random(); //创建随机数对象 for (int i=0; i<5; i++) { pet. Choice=the. Animals[select. next. Int(the. Animals. length)]; System. out. println("n. Your Choice: n"+pet. Choice); pet. Choice. sound(); } 不同的Animals对象自动匹配相 } 应的sound()方法。 }
class 类人猿 { private int n=100; void cry. Speak(String s) { System. out. println(s); } } class People extends 类人猿 { void computer(int a, int b) { int c=a*b; System. out. println(c); } void cry. Speak(String s) { System. out. println("**"+s+"* *"); } } class Example 4_21 { public static void main(String args[]) { 类人猿 monkey=new People(); monkey. cry. Speak("I love this game"); People people=(People)monkey; people. computer(10, 10); } } monkey. n=1000; monkey. computer(10, 10);
• 运行结果是什么? class A{ public A(){ System. out. println("String a"); } } class B extends A{ public B(){ System. out. println("String b"); } } class C extends B{ public C(){ System. out. println("String c"); } } public class Constructor. Test{ public static void main(String args[]){ C c = new C(); } }
常用的系统包 (1)java. lang——自动加载 (2)java. io (3)java. awt (4)java. awt. event (5)java. swing (6)java. util——高级类 (7)java. net (8)java. applet (9)java. sql (10)java. rmi——远程连接 (11)java. security——安全机制
包 的 示 例 package family; class Father{ //类Father装入包family String name; 定义一个包,名为family int age; public void drive(){ 定义了3个类: System. out. println(“I can drive. ”); Father、Son和 } } class Son{ //类Son装入包family Daughter,并放在 String name; 包family中 int age; public void play(){ System. out. println(“I can play basketball. ”); } } class Daughter{ //类Daughter装入包family String name; int age; public void paly(){ System. out. println(“I can play tableball. ”); } }
import语句示例 编写一个applet程序,实现在屏幕上显 示 “import语句的应用实例”。 import java. awt. *; import java. applet. Applet; public class Ex 7_3 extends Applet{ public void paint(Graphics g){ g. draw. String("import语句的应用实例", 20); } }
public class Ex 7_4{ public static void main(String arg[]){{ AClass a. Class=new AClass(); a. Class. ax=10; //Error a. Class. ay=20; //Error a. Class. print(); BClass b. Class=new BClass(); b. Class. bx=15; b. Class. by=25; b. Class. print(); } } package example; class AClass{ private int ax, ay; public void print() { int result; result=ax+ay; System. out. println("ax+ay="+result); } } class BClass{ public int bx, by; public void print() { int result; result=bx+by; System. out. println("bx+by="+result); } } 为什么 是错误 的?
Collection • Collection是在java. util包中的接口,其声明如下: public interface Collection • 常用方法: (1)public boolean add(Object o): 将对象添加到集合中 (2)public boolean contains(Object o)查找集合中是否含 有对象o (3)public boolean equals(Object o)判断集合是否等价 (4)public Iterator iterator()返回一个迭代器,用来访问 集合中的元素 (5)public boolean remove(Object o)删除集合中的对象o (6)public int size()返回集合中元素的个数 (7)public Object[] to. Array()以数组的形式返回集合中的 元素
- Slides: 30