类与对象的基础题: 1)编程实现:以电话Phone为父类(例:电话有本机号码、打电话、接电话等属性和功能,
class Phone{
private String phonenumber;
public void setPhonenumber(String phonenumber){
this.phonenumber=phonenumber;
}
public String getPhonenumber(){
return phonenumber;
}
public void recCall(){
System.out.println("接到一个电话");
}
public void telCall(){
System.out.println("拨出一个电话");
}
}class Fixedphone extends Phone{
private String phonenumber;//号码是私有,设置为private,不可继承
public void recCall(){
System.out.println("以"+this.phonenumber+"呼出了一个电话"); //重载了父类的recCall
}
}class Cordlessphone extends Fixedphone{
private String phonenumber;
public void info(){
System.out.println("这是无绳电话的信息");
}
}interface Moveable{
public void moveinfo();
}class Mobilephone extends Phone implements Moveable{
private String phonenumber;
public void moveinfo(){
System.out.println("我实现了可移动性");
}
}public class PhoneTest{
public static void main(String a[]){
Phone[] p=new Phone[5];
Phone p1=new Phone();
p1.setPhonenumber("123456789");
p[0]=p1;
Phone p2=new Phone();
p2.setPhonenumber("987654321");
p[1]=p2;
Mobilephone mp=new Mobilephone();
mp.setPhonenumber("11111");
p[2]=mp;
Fixedphone fp=new Fixedphone();
fp.setPhonenumber("22222");
p[3]=fp;
Cordlessphone cp=new Cordlessphone();
cp.setPhonenumber("33333");
p[4]=cp;
for(int i=0;i<p.length;i++){
System.out.println(p[i].getPhonenumber());
} p[4]=p[1];
System.out.println(p[4].getPhonenumber());
}} 写的不是很好,希望对你有帮助噶
//Student 类
class Student{
String name;
int age;
Student(String name,int age){
this.age=age;
this.name=name;
}
Student(){}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return age;
}
}
//Unergraduate类
class Undergraduate extends Student{
String specially;
Undergraduate(String n,int a,String s){
name = n;
age = a;
specially = s;
}
Undergraduate(){}
public void setSpecially(String specially){
this.specially = specially;
}
public String getSpecially(){
return specially;
}
}
//Postgraduate类
class Postgraduate extends Student{
String studydirection;
Postgraduate(String n,int a,String s){
name = n;
age = a;
studydirection = s;
}
Postgraduate(){}
public void setStudydirection(String studydirection){
this.studydirection = studydirection;
}
public String getStudydirection(){
return studydirection;
}
}
public class TestStudent{ //程序入口
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stu1 = new Student("Tom",19);
Student stu2 = new Student("Anna",20);
Postgraduate stu3 = new Postgraduate("John",25,"Computer Science");
Postgraduate stu4 = new Postgraduate("Lucy",24,"Chinese");
Undergraduate stu5 = new Undergraduate("Ansen",21,"Math");
Student[] data = ; //Student类的数组
System.out.println("NameAge");
System.out.println("--------------");
System.out.println(stu1.getName()+""+stu1.getAge());
System.out.println(stu2.getName()+""+stu2.getAge());
System.out.println(stu3.getName()+""+stu3.getAge()+""+stu3.getStudydirection());
System.out.println(stu4.getName()+""+stu4.getAge()+""+stu4.getStudydirection());
System.out.println(stu5.getName()+""+stu5.getAge()+""+stu5.getSpecially());
System.out.println("--------------");
data[0].setName("汤姆");
stu2.setName("约翰");
for(int i=0;i<data.length;i++)
System.out.println(data[i].getName());
}
}
private String phoneNumber;
public abstract void dial();
public abstract void hangup();
}
public class Mobilephone extends Phone {
public void dial() {
//实现
}
public void hangup() {
//实现
}
}
亥俩齐宏:[答案] class Phone{ private String phonenumber; public void setPhonenumber(String phonenumber){ this.phonenumber=... public void moveinfo(){ System.out.println("我实现了可移动性"); } }public class PhoneTest{ public static void main(String a[]){ ...
嘉峪关市17335965036: 1、类与对象的基础题: 1)编程实现:设计一个学生类Student,包含的属性有姓名name和年龄age. 由学生类派 - ?
亥俩齐宏: 怎么不全? public class Student { public String name; public int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }...
嘉峪关市17335965036: 类与对象的基础题: 1)编程实现:以电话Phone为父类(例:电话有本机号码、打电话、接电话等属性和功能, - ?
亥俩齐宏: 期待看到有用的回答!
嘉峪关市17335965036: java程序设计类与对象的基础题,求程序代码! - ?
亥俩齐宏: class Simple{private int A;//属性1private double B;//属性2private String C;//属性3//set();get()方法public int getA() {return A;}public void setA(int a) {A = a;}public double getB() {return B;}public void setB(double b) {B = b;}...
嘉峪关市17335965036: 用JAVA编程 类与对象的基础题 - ?
亥俩齐宏: //Student 类 class Student{ String name; int age; Student(String name,int age){ this.age=age; this.name=name; } Student(){} public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setAge(int age)...
嘉峪关市17335965036: Java编程 - ?
亥俩齐宏: /*1、类与对象的基础题: 1)编程实现:以电话Phone为父类(例:电话有本机号码、打电话、接电话等属性和功能, 当然还有一些其它的特性), 移动电话Mobilephone和固定电话Fixedphone为两个子类, 并使移动电话实现接口:可移动...
嘉峪关市17335965036: java简单题 - ?
亥俩齐宏: public class Phone { //电话号码 int code; /** * 打电话 */ public void call() { } /** * 接电话 */ public void receive() { } } public interface Movable { /** * 接口,可移动 * @return */ public boolean movable(); } public class MobilePhone extends Phone ...
嘉峪关市17335965036: c++类与对象的一个编程题目?
亥俩齐宏: 你好,这道题的答案是:#include<iostream.h>class Date{public: Date(){year=2006;month=1;day=1;} Date(int y,int m,int d){ year=y; month=m; day=d; } //构造函数; void setDate(int y,int m,int d) //设置函数值函数; {year=y;month=m;day=d;} int ...
嘉峪关市17335965036: C++类与对象的题,计算矩形类面积和周长 - ?
亥俩齐宏: #include#define PI 3.1415926; using namespace std; class Shape //抽象类的 定义 { public: virtual double GetArea() = 0; //纯虚函数 virtual double GetPerim() = 0; //纯虚函数 }; class Rectangle : public Shape //矩形类,公有继承 { public: Rectangle...
嘉峪关市17335965036: c++编程题 类与对象?
亥俩齐宏: #include <iostream.h> #include <string> class Worker{ private: //1)私有数据成员 unsigned int id;//: 职工号. char name[11];//:姓名 float salary;//: 工资.(有一符号常量为工资最低值,设为2000) int level;//: 技术等级(1~20级),1...