
正文
Java继承代码考试题 java继承笔试题
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
简单的java 编程题 关于继承
package javaapplication4;
public class Rect {
protected int length;/////这个地方不能变成私有属性,因为后面继承的类也需要继承它。
protected int width;
public Rect(int length,int width)
{
this.length=length;
this.width=width;
}
public void setLength(int length)
{this.length=length;br }
public void setWidth(int width)
{this.width=width;br }
public int getLength()
{return length;br }
public int getWidth()
{return width;br }
public int getArea()
{return length*width;br }
public int getCycle()
{return (length+width)*2;br }}
////////////////////////////////////////////////////////////////////////////////////////////////////////
package javaapplication4;
public class PlainRect extends Rect
{///此类并未继承父类的长宽属性,所以父类的设计是不合理的。应将其属性改为protected.
protected int startX,startY;
public PlainRect(int length,int width,int startx,int starty)
{
super(length,width);
startX=startx;
startY=starty;
}
public PlainRect()
{
super(0,0);
startX=0;
startY=0;
}
public boolean isInside(double x,double y)
{
boolean b=false;
if(xstartXxstartX+length)
if(ystartYystartY+width)
b=true;
return b; }}
////////////////////////////////////////////////////////////////////////////////////////////////////////
package javaapplication4;
public class Main {
public static void main(String[] args) {
PlainRect Pr1=new PlainRect(20,10,10,10);
System.out.println("面积为:"+Pr1.getArea());
System.out.println("周长为:"+Pr1.getCycle());
System.out.println("点(25.5,13)"+(Pr1.isInside(25.5, 13)?"在":"不在")+"此方形内");
} }
相关问答
Q1: java继承与多态的题目求解答
public class PhoneDemo {
public static void main(String[] args) {
Phone p1=new OldPhone();
Phone p2=new NewPhone();
show(p1);
System.out.println("\n----------分割线-----------\n");
show(p2);
}
private static void show(Phone p) {
p.call();
p.message();
if(p instanceof Iplay) {
((Iplay) p).game();
}
}
}
class Phone{
public void call(){
System.out.println("古董---Call....");
}
public void message() {
System.out.println("古董---message....");
}
}
interface Iplay{
public void game();
}
class OldPhone extends Phone{
}
class NewPhone extends Phone implements Iplay{
public void call(){
System.out.println("时尚---Call....");
}
public void message() {
System.out.println("时尚---message....");
}
public void game() {
System.out.println("望着农药真好玩!");
}
}
Q2: java 继承题目
//继承
public class test {
public static void main (String args[]){
Manager m=new Manager ("小明",5000,"北京","java工程师"); //创建一个经理对象
System.out.println("经理的信息:");
m.show(); //输出信息
Director d =new Director ("小娃",6000,"广州",500); //创建一个director对象
System.out.println("\n Director 的信息:"); //调用函数输出信息
d.show();
}
}
class Employee{ //创建Employee类
String name ;
float basic;
String address;
Employee (String nam,float bas,String add){ //初始化
name=nam;
basic=bas;
address=add;
}
public void show (){
System.out.println("名字:"+this.name);
System.out.println("基本工资::"+this.basic);
System.out.println("地址:"+this.address);
}
}
class Manager extends Employee{ //Manager类继承了父类的属性和方法
String department;
Manager (String nam, float bas, String add,String dep) {
super(nam, bas, add); //使用super关键字
department=dep;
}
public void show() {
super.show();
System.out.println("部门:"+department);
}
}
class Director extends Employee{ //同上一个类一样的
double transportAllowance;
Director(String nam, float bas, String add,double tra) {
super(nam, bas, add);
transportAllowance=tra;
}
public void show() {
super.show();
System.out.println("交通补贴:"+transportAllowance);
}
}
Q3: 关于JAVA继承类的一道笔试题
构造函数都是先执行构造 再执行其他的.像你的class X 你去用无参构造器new一个X时,他会先调用你重写的无参构造器,如果自己没写jvm会去调用默认的,默认的无参数构造器是默认就存在的不论你写不写.执行构造函数时他会先执行你构造方法的代码,执行完成后才会去执行其他方法 去生成这个类的属性 执行静态代码块,class X中的 Y b=new Y(); 这段代码你放哪里其实只是声明了一个X的成员变量而已,放哪里执行顺序都是一样的
Q4: 请教两道JAVA的编程题目。关于继承与重载,请按下列要求编写代码。
public class Good {
String goodname;
double goodprice;
boolean isMem;
public Good(String name,double price){
this.goodname=name;
this.goodprice=price;
this.isMem=false;
}
public void setName(String name){
this.goodname=name;
}
public void setPrice(double price){
this.goodprice=price;
}
public void setMem(){
this.isMem=!this.isMem;
}
public void discount(){
System.out.println(this.goodprice);
}
}
public class Milk extends Good {
double memprice;
public Milk(String name, double price,double memprice) {
super(name, price);
this.memprice=memprice;
// TODO Auto-generated constructor stub
}
public void setMemPrice(double memprice){
this.memprice=memprice;
}
public void discount(){
if(isMem)
System.out.println(this.memprice*0.8);
else
System.out.println(this.goodprice*0.8);
}
public static void main(String args[]){
Milk test=new Milk("牛奶",3,2.6);
test.discount();
test.setMem();
test.discount();
}
}
关于Java继承代码考试题和java继承笔试题的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






