面向对象技术 第七章继承和多态.jsp

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

//使用import引入已经定义的,属性为public的 Account7_3类 ,引用其方法 import bag.Account7_3 ; //引用公共类Account7_3 public class Useraccount7_3 { public static void main(String args[]) { Account7_3 myaccount=new Account7_3(); //创建类对象 myaccount.name="张三 "; 通过import引用在本类 myaccount.address="银川市北京路100号 "; 中可以直接建立相应的 myaccount.balance=123.40; 类对象,并应用其中的 myaccount.display(); 具有public权限的变量 } } 和方法
不 想 让 其 它 类 访 问 就 设 置 为 私 有 权 限
// 创建私有方法,在构造方法中引用方法,私有变量的引用权限 class Scope7_4{ private int x=10; Scope7_4( ) { int x=1000; a( ); b( ); } private void a( ) { int x=200; x*=4; System.out.println("A of a method is "+x); } private void b( ) { x*=2; System.out.println("B of a method is "+x); } } public class Samp7_4 { public static void main(String args[]) { int x=100; System.out.println("x of main method is "+x); Scope7_4 b1=new Scope7_4( ); // b1.a( );//方法a()已被构造函数初始化 // b1.b( ); System.out.println("x of main method is "+x); } }
第七章 继承与多态
面向对象的程序设计以类作为基本处理单元,对象 是类的实例。面向对象程序设计的重要特征是具有 封装性、多态性和继承性。 所谓封装表现在以下几个方面 1、在类的定义中设置对对象中的成员变量和方法进行 访问的权限。 2、提供一个统一供其它类引用的方法。 3、其它对象不能直接修改本对象所拥有的属性和方法。
编译此程序,要给Account7_3.class文件定位:附件一 D:\Javakj\Javalt>Javac –classpath d:\Javakj\Javalt Useraccount7_3.Java 程序运行结果 name:张三 address:西安市兴庆路19号 balance:123.4
// 定义多个同名方法, 实现的重载方法 // 这些方法具有相同的名称,但有不同的参数和不同的定义 public class Calculate7_7 { public static int abs1(int x) {//定义整数运算的方法 int y; y=x; if(y<0) y=-y+5; return y; } public static double abs1(double x) {//定义双精数运算的方法 double y; y=x; if(y<0) y=-y*2; return y; } public static void main(String args[] ) { int m=-25; double n=-8.27; System.out.println("m="+m+" "+abs1(m)); System.out.println("n="+n+" "+abs1(n)); } }
// 将x17_6类独立建成X17_6.Java类文件,放入bag包中以便其他程序直接调用 //编译命令为 D:\Java程序设计例题> Javac -d D:\Java程序设计例题 X17_6.Java package bag; public class X17_6{ public X17_6(){ System.out.println("this is a construct"); } public void doing( ) { System.out.println("could you tell me"); } // 引用包bag中的X17_6.Java类文件 } import bag.X17_6; public class X27_6{ 未设定public或 static void store(){ private等访问 System.out.println("this is a store processing"); 权限的均为 } friendly,包内 public static void main(String args[]) 的所有类可以 { 访问它们,包 X17_6 y=new X17_6(); 外的不行 y.doing(); store(); } }
//引用私有方法和修改私有成员变量 class def{ private int x,y,z=0; public void method1(){ int x=3,y=4,z; z=x*y; System.out.println("z="+z); } public void show() { System.out.println("z="+z); } } public class Change7_2{ public static void main(String args[]) { int z=20; def ss=new def(); ss.method1(); ss.show(); } }
多态机制
多态机制是面向对象程序设计的一个重要特征。Java 的 多态机制主要表现在方法的重载、构造方法的重载以及方法 的覆盖几个方面。 多态的特点是采用同名的方式,根据调用方法时传送的参 数的多少以及传送参数类型的不同,调用不同的方法,这样对 于类的编制而言,可以采用同样的方法获得不同的行为特征。 所谓方法的重载,就是在类中创建了多个方法,它们具有相 同的名称,但有不同的参数和不同的定义。在调用方法时依据 其参数个数及类型自动选择相匹配的方法去执行。达到各种对 象处理类似问题时具有统一的接口之目的。
// 定义类的protected 方法,通过创建对象来引用此方法 class Max7_5{ private int x,y; protected int play(int s,int t) { 使用 int m; protected定 x=s; y=t; 义的成员变量 m=(x>y)?x/y:y/x; 或方法具有二 return m; 重性,类本身、 } } 子类或包中的 public class GetNum7_5 方法可以访问 { 它们,而其它 public static void main(String args[]) { 类没有访问权 int result; 限 Max7_5 ss=new Max7_5(); result=ss.play(5,45); System.out.println(" result= "+result); } }
这里定义了 两个构造方 法,根据类 实例初始化 时给定的参 数决定使用 哪一个
public String toString( )
{ return “[“+x+”’”+y+”]”;} }
// 创建私有方法,在构造方法中引用方法,私有变量的引用权限 class Scope7_4{ private int x=10; Scope7_4( ) { int x=1000;} private void a( ) { int x=200; x*=4; System.out.println("A of a method is "+x); } private void b( ) { x*=2; System.out.println("B of a method is "+x); } } pubLeabharlann Baiduic class Samp7_4 { public static void main(String args[] ) { int x=100; System.out.println("x of main method is "+x); Scope7_4 b1=new Scope7_4( ); b1.a( );//调用私有方法出错 b1.b( );//调用私有方法出错 System.out.println("x of main method is "+x); } }
public 修饰的类是公共类,修饰的变量是公共变量, 修饰的方法是公共方法。这些类、变量、方法可以 供其它类访问。 一个Java程序文件只能定义一个public类,而且程 序文件必须与它同名。为其它程序共享的类须经过 编译进行打包,形成一个包文件,然后用import语 句加以引用。
打包是在Java程序编译时进行的,注意参数-d 被编译程序所在路径 Javac –d 被编译程序所在路径 被编译程序 名.Java 被编译程序的第一行用package 包名 ;给出它被打 入的包,该包与系统创建的子目录同名。
// 定义 属性为public的 Account7_3类 ,及其引用其方法,使用编译命令 // D:\Java程序设计例题> Javac -d D:\Java程序设计例题 Account7_3.Java package bag ; // 创建包bag ,将类放入bag包中 public class Account7_3 { public String name; public String address; 这里创建了一个包其 public double balance; 名字叫bag,并且将编 public void display() 译后的 { Account7_3.class 文 System.out.print(" name:"); 件装入包中 System.out.println(name); System.out.print(" Address:"); System.out.println( address); System.out.print(" balance:"); System.out.println( balance); } }
对象变量的四种访问权限 public, protected, private , friendly.
//引用私有方法和修改私有成员变量 class def{ private int x,y,z=0; private void method1(){ int x=3,y=4,z; z=x*y; System.out.println("z="+z); } public void show() { System.out.println("z="+z); } } public class Change7_1{ public static void main(String args[]) { def ss=new def(); ss.method1();//调用私有方法出错 ss.z=12;//修改私有变量出错 ss.show(); } }
//定义构造方法的重载
Public class Point{
protected int x,y; public Point( ){setPoint(0,0);} public Point(int a,int b){setPoint(a,b);} piblic void setPoint(int a,int b){ x=a; Y=b; } public int getx( ){return x;} public int gety( ){return y;}
相关文档
最新文档