⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 6.txt

📁 java2应用开发指南第一版
💻 TXT
📖 第 1 页 / 共 2 页
字号:
	//子类的公共成员方法
   public String getDateOfMade()
	{
		return dateOfMade;
	}
}



例程6-15
// HerihacyDemo.Java
class HerihacyDemo
{
	public static void main(String args[])
	{
		//创建类的实例
		Automobile aAutomobile = new Automobile(“红旗”,”2001/01/01”);
		//初始化类成员
		aAutomobile.speed = 160;
		//初始化类成员
aAutomobile.maintanceRecord = “无”;
System.out.println(“车辆名称:”+ aAutomobile.getModel());
System.out.println(“生产日期:”+ aAutomobile.getDateOfMade());
System.out.println(“最高时速:”+ aAutomobile.speed);
System.out.println(“维修记录:”+ aAutomobile. maintanceRecord);
	}
}



例程6-16
//B.Java
//父类A
class A
{
	//父类公共成员变量
	public int a;
	public int b;
	//父类私有类型成员变量
	private int c;
	public A()
	{
	}
}
//子类继承父类
class B extends A
{
	public int d;
	public B()
	{
	}
	public static void main(String args[])
	{
		B aB = new B();
		//访问父类的公共类型成员变量
		aB.a = 1;
		//访问父类的公共类型成员变量
		aB.b = 2;
		//不能访问父类A的私有类型的数据成员c
		//aB.c = 3;
		aB.d = 4;
	}
}



例程6-17
// MultiConstructor.Java
class MultiConstructor
{
  //成员变量
   int variable1;
   int variable2;
   int variable3;
  //构造方法1
   public MultiConstructor ()
   {
	}
  //构造方法2
   public MultiConstructor (int variable1)
   {
		this.variable1 = variable1;
   }
  //构造方法3
   public MultiConstructor (int variable1, int variable2)
   {
		this.variable1 = variable1;
		this.variable2 = variable2;
	}
  //构造方法4
   public MultiConstructor (int variable1, int variable2, int variable3)
   {
		this.variable1 = variable1;
		this.variable2 = variable2;
		this.variable3 = variable3;
	}
}



例程6-18
// MultiFunctionDemo.Java
class MultiFunction
{
	int a,b;
  //成员方法一
   int memberFunction ()
   {
		return a+b;
   }
  //成员方法二
   int memberFunction (int c)
   {
		return a+b+c;
   }
  //成员方法三
   int memberFunction (int c,int d)
   {
		return a+b+c+d;
   }
  //构造方法
   MultiFunction (int a,int b)
   {
		this.a = a;
       this.b = b;
   }
}
public class MultiFunctionDemo
{
	public static void main(String args[])
	{
       //实例化
		MultiFunction  aMultiFunction = new MultiFunction (1,2);
       //调用成员方法
       int a = aMultiFunction.memberFunction();
		System.out.println(a);
       //调用成员方法
       int b = aMultiFunction.memberFunction (3);
		System.out.println(b);
       //调用成员方法
       int c = aMultiFunction.memberFunction (3,4);
		System.out.println(c);
	}
}



例程6-19
//interface定义
interface anyone
{
	final int PI=3.1416;
	void setNumber(int number);
	int  getNumber();
}
//另外一个interface的定义
interface anyother
{
	void setString(String strName);
	int  getString();
}
//实现上面接口的类
class Implementer implement anyone,anyother
{
	//类Implementer的成员变量
	int number;
	String strName;
	//类的构造方法
	public Implement()
	{
	}
	//实现接口方法setNumber
	void setNumber(int number)
	{
		this.number = number;
	}
	//实现接口方法setString
	void setString(String strName)
	{
		this.strName = strName;
	}
	//实现接口方法getNumber
	int getNumber()
	{
	}
	//实现接口方法getString
	String getString()
	{
	}
}



例程6-20
//interface定义
interface anyone
{
	final int PI=3.1416;
	void setNumber(int number);
	int  getNumber();
}
//另外一个interface的定义
interface anyother
{
	void setString(String strName);
	int  getString();
}
//实现上面接口的类
abstract class Implementer implement anyone,anyother
{
	//类Implementer的成员变量
	int number;
	String strName;
	//类的构造方法
	public Implement()
	{
	}
	//实现接口方法setNumber
	void setNumber(int number)
	{
		this.number = number;
	}
	//实现接口方法setString
	void setString(String strName)
	{
		this.strName = strName;
	}
}



例程6-21
//Herihacy.Java
//父接口
interface Parent
{
	//父接口方法
	int method1();
	int method2();
}
//子接口
interface Child extends Parent
{
	//子接口方法
	void method3();
}
class Herihacy implements Child
{
	//构造方法
	public Herihacy()
	{
	}
	public int method1()
	{
		return 2;
	}
	public int method2()
	{
		return 3;
	}
	public void method3()
	{
		System.out.println("method3");
	}
	public static void main(String args[])
	{
		Herihacy aHerihacy = new Herihacy();
		int a = aHerihacy.method1();
		System.out.println("method1:"+a);
		int b = aHerihacy.method2();
		System.out.println("method2:"+b);
		aHerihacy.method3();
	}
}



例程6-22
//Demo1.Java
package p1;
class Demo1
{
   //公共类型的成员变量
   public int a;
	//公共类型的成员方法
	public void method()
	{
		System.out.println();
	}
}
//Demo2.Java
package p1;
class Demo2
{
	public static void main(String args[])
	{
      //实例化Demo1
		Demo1 aDemo1 = new Demo1();
      //访问Demo1中的公共成员方法
		aDemo1.method();
      //访问Demo1中的公共成员变量
		aDemo1.a = 10;
       int a = aDemo1.a;
		System.out.println(“Demo1中的公共成员变量a的值:”+a);
	}
}



例程6-23
// Servre.Java
package Server;
//定义Server类
class Server
{
   //公共类型的成员变量
   public int a;
	//公共类型的成员方法
	public void method()
	{
		System.out.println();
	}
}
//定义另一个类Other
class Other
{
	public Other()
	{
	}
}
//Client.Java
package Client;
//引入其他包中的类
import Server.Server;

class Client
{
	public static void main(String args[])
	{
      //实例化Servre
		Servre a Servre = new Servre ();
      //访问Servre中的公共成员方法
		a Servre.method();
      //访问Servre中的公共成员变量
		a Servre.a = 10;
       int a = Servre.a;
		System.out.println(“Servre中的公共成员变量a的值:”+a);
	}
}



例程6-24
// Servre.Java
package Servre;
//定义Server类
class Servre
{
   //公共类型的成员变量
   public int a;
	//公共类型的成员方法
	public void method()
	{
		System.out.println();
	}
}
//定义另一个类Other
class Other
{
	public int b;
	public Other(int b)
	{
		this.b = b;
	}
	public int getB()
	{
		return b;
	}
}
//Client.Java
package Client;
//引入其他包中的类
import Server.*;

class Client
{
	public static void main(String args[])
	{
      //实例化Servre
		Servre aServre = new Servre ();
      //访问Servre中的公共成员方法
		a Servre.method();
      //访问Servre中的公共成员变量
		a Servre.a = 10;
       int a = Servre.a;
		System.out.println(“Servre中的公共成员变量a的值:”+a);

		//实例化包Server中的类Other
		Other another = new Other(5);
		//调用类Other的方法
		int b = another.getB();
		System.out.println(b);
	}
}



例程6-25
// Servre.Java
package Servre;
//定义Server类
class Servre
{
   //公共类型的成员变量
   public int a;
	//公共类型的成员方法
	public void method()
	{
		System.out.println();
	}
}
//定义另一个类Other
class Other
{
	public int b;
	public Other(int b)
	{
		this.b = b;
	}
	public int getB()
	{
		return b;
	}
}
//Client.Java
package Client;
//引入其他包中的类

class Client
{
	public static void main(String args[])
	{
      //实例化Servre
		Servre.Server aServre = new Servre ();
      //访问Servre中的公共成员方法
		a Servre.method();
      //访问Servre中的公共成员变量
		a Servre.a = 10;
       int a = Servre.a;
		System.out.println(“Servre中的公共成员变量a的值:”+a);

		//实例化包Server中的类Other
		Server.Other another = new Other(5);
		//调用类Other的方法
		int b = another.getB();
		System.out.println(b);
	}
}



例程6-26
//Circle.Java
class Circle
{
   //定义程序运行常量π的值
	public static final double PAI = 3.14;
   //定义成员变量:圆的半径
   public double radius;
   //类的构造函数
   public Circle( double radius)
   {
		this.radius = radius;
   }
   //类的方法成员
   public double getArea()
   {
		return this.radius*this.radius*PAI;
   }
	public static void main(String[] args)
	{
	  Circle aCircle = new Circle( 2.0 );
	  double area = aCircle.getArea();
	  System.out.println("圆的面积是:"+ area);
	}
}



例程6-27
//B.Java
//类A
class A
{
   //公共类型的成员变量
	public int n;
	
	public A()
	{
	}
   	public A(int n)
	{
		this.n = n;
	}
    int method()
    {
		return n;
    }
}
//类B
class B extends A
{
	public B()
	{
		super(5);
	}
	public static void main(String args[])
	{
	    //实例化A
		A aInstance = new A();
    	//为类A的公共类型成员变量赋值
		aInstance.n = 10;
		//访问类A中的方法
		int b=aInstance.method();
		System.out.println("类A中的成员变量:"+b);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -