面向对象课后题

本文主要介绍了Java面向对象编程的基础知识,包括构造方法的使用、类的结构和成员、对象创建等。提供了选择题、简答题和编程题,涵盖了类的构造、成员变量、方法、继承和多态等核心概念。通过解答这些题目,读者可以巩固对Java面向对象编程的理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(一)选择题

  1. this在程序中代表的是?(A)
    A.类的对象 B.属性 C.方法 D.父类
  2. 设 A为已定义的类名,下列声明并创建A类的对象a的语句中正确的是? (A) A.A a=new A( ); B.public A a=A( ); C.A a=new class( ); D.a A;
  3. 下列对构造方法的调用方式的描述中正确的是?(A)
    A.使用new调用 B.使用类名调用 C.使用对象名调用 D.调用方法为对象名.方法名
  4. 设 X 、Y 均为已定义的类名,下列声明类X的对象x1的语句中正确的是?(A) A.X x1; B.X x1= X ( ); C.X x1=new Y( ); D.int X x1;
  5. 定义类头时,不可能用到的关键字是?© A.class B.public C.extends D.static
  6. 下面关于类的结构的说法正确的是 ?(B) A.类只能包含一个构造方法 B.类可以没有属性 C.类只能包含方法 D.类只能包含属性
  7. 下面哪个关键字在定义类头时用不到?(CD) A.class B.public C.extends D.int
  8. 下面类的定义中结构正确的是 ?(A) A.class A B.class 2A C.int class A D.public class A
  9. 关于java源文件下列说法正确的是?(A)
    A.一个java源文件中只能有一个public 修饰的类
    B.一个java源文件中只能有一个缺省的类
    C.一个java源文件中可以有多个protected修饰的类
    D.一个java源文件中可以有多个protected修饰的类
    10. 有一个类A,以下为其构造方法的声明,其中正确的是?(B) A.void A(int x) B.A(int x) C.a(int x) D.void a(int x)
    11. 下列构造方法的调用方式中,正确的是 © A.按照一般方法调用 B.由用户直接调用 C.只能通过new自动调用 D.不用调用,自动执行
    12. 设i , j为类X中定义的double型变量名,下列X类的构造方法中不正确的是?(A) A.double X(double k ) B.X( ) C.X(double m, double n ) D.X(double k )
    13. 以下关于构造函数的描述错误的是 ?(A,D)
    A.构造函数的返回类型只能是void型。
    B.构造函数是类的一种特殊函数,它的方法名必须与类名相同。
    C.构造函数的主要作用是完成对类的对象的初始化工作。
    D.一般在创建新对象时,系统会调用构造函数。//我创建对象,调用构造函数
    14. 面向对象的特点是?(A) A.继承 封装 多态 B.继承 接口 对象 C.消息 继承 类 D.接口 继承 类
    15. 下列不属于面向对象的三大特征的是 ?(B) A.继承 B.方法 C.封装 D.多态
    16. 面向对象程序不包含下面的哪个组成部分?(D) A.类 B.对象 C.接口 D.程序员
    17. 下列关于类的定义的选项中,哪些可以通过new Thing()方法来创建该类的一个对象? (AC)//当Thing()为构造器时可以
    A. public class Thing { public Thing() {}}
    B. public class Thing { public Thing(void) {} }
    C. public class Thing { public Thing() {}}
    D. public class Thing { public Thing(String s) {} } }
    E. public class Thing { public void Thing(String s) {} }

(二)简答题

  1. 写出程序的运行结果。
 public class C { 
 public static void main(String args[]) { 
String s1 = new String("Hello!"); 
String s2 = new String("I love JAVA."); 
A s = new A(s1, s2);
 System.out.println(s1 + s2);
 System.out.println(s.toString()); 
}
 }
 class A {
 String s1;
 String s2; 
A(String str1, String str2) {
 s1 = str1; 
 s2 = str2;
 str1 = "No pain ,";
 str2 = "no gain!"; 
System.out.println(str1 + str2);
 } 
public String toString() {
 return s1 + s2;
 } 
}

No pain ,no gain!
Hello!I love JAVA.
Hello!I love JAVA.

  1. 写出下列程序的输出结果。
 class Cruncher { 
 void crunch(int i) { 
 System.out.print("int"); 
 } 
void crunch(String s) { 
System.out.print("String"); 
} 
public static void main(String args[]) { 
 Cruncher crun = new Cruncher();
 char ch = 'p'; int c = 12;
 crun.crunch(ch); 
 System.out.print(";"); 
 crun.crunch(c);
 } 
}

int;int

  1. 下面是同学写的Teacher类,请指出其中的错误。 static class Teacher { string name; int age; void Teacher(int n, int a) { name = n; age = a; return age; } }
    去掉static(外部类不能有static),String,构造方法中去掉void,去掉return age;
    class Teacher { String name; int age;
    Teacher(int n, int a) {
    name = n; age = a;
    }
    }
  2. 下面是一个类的定义,请完成程序填空。
    public class Myclass
    {
    int x, y; Myclass ( int i, int j) // 构造方法 { x=i; y=j; } }
  3. 下面方法的功能是判断一个整数是否为偶数,将程序补充完整。
    public boolean isEven(int a) { if(a%2==0) return true; else return false; }
  4. 定义求绝对值的方法abs(),利用方法的重载实现既能求整数的绝对值, 又能求浮点数的绝对值。
 public class Test1 { 
 	public static int abs(int n){ 
 	if(n<=0)
	return -n;
 else return n; } 
 public static double abs(double n)
 { if(n<=0)
	return -n;
 else return n; } }
  1. 在父类中定义了printA()方法,输出“A”,子类中覆盖printA()方法输出“AA”
 class Father {
 	int a; 
 	public void printA(){ 
 	System.out.println("A"); 
 	} 
 } 
 class Child extends Father{
	public void printA(){
 	 System.out.println("AA"); 
  	} 
  }
  1. 创建一个Circle类,此类中包括一个半径属性radius和一个计算面积的方法findArea。在main方法中创建Circle类的对象c,并计算半径为5的圆的面积。
 public class Circle { 
 double radius; 
 double findArea(){ return 3.14*radius*radius; } 
 	public static void main(String[] args){
 	Circle c=new Circle();
	c.radius=5.0;
	System.out.println(c.findArea()); 
	} 
}
  1. 创建一个Telephone类,属性有电话号码number,还有2个构造方法,其中一个没有参数,一个带参数。
public class Telephone { 
	String number;
  	Telephone(){
  	number="041184835202"; 
	}
  	Telephone(String number){
  	this.number=number;
	}
 }

(三)编程题

  1. 定义一个点类Point,包含2个成员变量x、y分别表示x和y坐标,2个构造方法Point()和Point(int x0,y0),以及一个movePoint(int dx,int dy)方法实现点的位置移动,创建两个Point对象p1、p2,分别调用movePoint方法后,打印p1和p2的坐标。
public class T1 {
	/* 1.  定义一个点类Point,包含2个成员变量x、y分别表示x和y坐标,2个构造方法Point()和Point(int x0,y0),
	 以及一个movePoint(int dx,int dy)方法实现点的位置移动,创建两个Point对象p1、p2,分别调用
	 movePoint方法后,打印p1和p2的坐标。*/
	public static void main(String[] args) {
		Point p1=new Point();
		Point p2=new Point(1,8);
		p1.movePoint(1, 9);
		p2.movePoint(7, 1);
	}
}
class Point {
	int x,y;
	
	Point(){
		x=0;
		y=0;
	}
	Point(int x0,int y0){
		x=x0;
		y=y0;
	}
	public void movePoint(int dx,int dy){
		x=x+dx;
		y=y+dy;
		System.out.println(x+","+y);
	}
	
}
  1. 定义一个矩形类Rectangle: a) 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。 b) 有2个属性:长length、宽width c) 通过构造方法Rectangle(int width, int length),分别给两个属性赋值 d) 创建一个Rectangle对象,并输出相关信息。
public class T2 {

	public static void main(String[] args) {
		Rectangle r1=new Rectangle(2, 8);
		Rectangle r2=new Rectangle(4, 3);
		r1.getArea();
		r1.getPer();
		r1.showAll();
		r2.showAll();
	}

}
class Rectangle{
	double length;
	double width;
	
	double area;
	double per;
	public Rectangle(double length, double width) {
		this.length = length;
		this.width = width;
		area=length*width;
		per=2*(length+width);
	}
	public void getArea(){
		System.out.println("面积:"+area);
	}
	public void getPer(){
		System.out.println("周长:"+per);
	}
	public void showAll(){
		
		System.out.println("长"+length+"宽"+width);
		System.out.println("面积:"+area);
		System.out.println("周长:"+per);
	}
}
  1. 定义一个笔记本类,该类有颜色(char)和cpu型号(int)两个属性。 a) 无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值; b) 输出笔记本信息的方法 c) 然后编写一个测试类,测试笔记本类的各个方法。
public class T3 {

	public static void main(String[] args) {
		NotebookComputer n1=new NotebookComputer('R', 123);
		n1.print();
	}
	
}
class NotebookComputer{
	char color;
	int cpu;
	public NotebookComputer() {
		
	}
	public NotebookComputer(char color, int cpu) {
		this.color = color;
		this.cpu = cpu;
	}
	public void print() {
		System.out.println("颜色"+color);
		System.out.println("cpu型号"+cpu);
	}
}
  1. 设计一个类Student,该类包括姓名、学号和成绩。设计一个方法,按照成绩从高到低的顺序输出姓名、学号和成绩信息。
public class PracticeStudent {
public static void main(String[] args) {
		Student s1=new Student("大乔", 1,100 );
		Student s2=new Student("小白", 2,67 );
		Student s3=new Student("莹", 3,97 );
		Student s4=new Student("空", 4,95 );
		Student[] students={s1,s2,s3,s4};
		show(students);
	}
	public static void show(Student[] students) {
		Student stu;
		for (int i = 0; i <students.length-1; i++) {
			for (int j = 0; j < students.length-i-1; j++) {
				if (students[j].score<students[j+1].score) {
					stu=students[j];
					students[j]=students[j+1];
					students[j+1]=stu;
				}
			}
			
		}
		for(Student i:students){
			i.show();
		}
		
	}

}
class Student {
	String name;//属性 
	int stuNo;
	double score;
	public Student(String name,int stuNo,double score) {
		this.name=name;//this代表当前对象
		this.stuNo=stuNo;
		this.score=score;
	}
	public void show(){
		System.out.println(name+"	学号:"+stuNo+"	成绩:"+score);
	}

}
  1. 定义一个人类Person:定义一个方法sayHello(),可以向对方发出问候语“hello,my name is XXX” ,有三个属性:名字、身高、体重 。
public class T5 {
	/*. 定义一个人类Person:定义一个方法sayHello(),可以向对方发出问候语
	 * “hello,my name is XXX” ,有三个属性:名字、身高、体重 。*/
	public static void main(String[] args) {
		Person p1=new Person();
		p1.name="小白";
		p1.sayHello();
	}

}
class Person{
	String name;//属性 
	int height;
	int weight;
	

	public void sayHello() {
		System.out.println("hello,my name is "+name);
	}
}
  1. 定义一个PersonCreate类:创建两个对象,分别是zhangsan,33岁,1.73;lishi,44,1.74 ,分别调用对象的sayHello()方法。
public class T6 {
	public static void main(String[] args) {
		PersonCreate p1=new PersonCreate("zhangsan", 33, 1.73);
		PersonCreate p2=new PersonCreate("lishi", 44, 1.74);
		p1.sayHello();
		p2.sayHello();

	}

}
class PersonCreate{
	String name;//属性 
	int age;
	double height;
	
	public PersonCreate(String name, int age, double height) {
		this.name = name;
		this.age = age;
		this.height=height;
	}

	public void sayHello() {
		System.out.println("hello,my name is "+name);
	}
}
  1. 定义一个人类Person:定义一个方法sayHello(),可以向对方发出问候语“hello,my name is XXX”,有三个属性:名字、身高、体重,通过构造方法,分别给三个属性赋值。
public class T5 {
	/*. 定义一个人类Person:定义一个方法sayHello(),可以向对方发出问候语
	 * “hello,my name is XXX” ,有三个属性:名字、身高、体重 。*/
	public static void main(String[] args) {
		Person p1=new Person("小白", 168, 40);
		Person p2=new Person("大白", 180, 60);
		p1.sayHello();
		p2.sayHello();
	}

}
class Person{
	String name;//属性 
	int height;
	int weight;
	
	public Person(String name, int height, int weight) {
		this.name = name;
		this.height = height;
		this.weight = weight;
	}

	public void sayHello() {
		System.out.println("hello,my name is "+name);
	}
}
  1. 定义一个Constructor类: 创建两个对象,分别是zhangsan,33岁,1.73;lishi,44,1.74 ,分别调用对象的sayHello()方法。
public class T8 {
	public static void main(String[] args) {
		Constructor p1=new Constructor("zhangsan", 33, 1.73);
		Constructor p2=new Constructor("lishi", 44, 1.74);
		p1.sayHello();
		p2.sayHello();
	}
}
class Constructor{
		String name;//属性 
		int age;
		double height;
		
		public Constructor(String name, int age, double height) {
			this.name = name;
			this.age = age;
			this.height=height;
		}

		public void sayHello() {
			System.out.println("hello,my name is "+name);
		}
}
  1. 定义一个汽车类Vehicle,要求如下: a) 属性包括:汽车品牌brand(String类型)、颜色color(String类型)和速度speed(double类型),并且所有属性为私有。 b) 至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。 c) 为私有属性提供访问器方法。注意:汽车品牌一旦初始化之后不能修改。 d) 定义一个一般方法run(),用打印语句描述汽车奔跑的功能。定义测试类VehicleTest,在其main方法中创建一个品牌为“benz”、颜色为“black”的汽车。
public class VehicleTes {

	public static void main(String[] args) {
		Vehicle v1=new Vehicle("benz", "black");
		v1.run();
	}

}
class Vehicle{
	private final String brand;
	private String color;
	private double speed;
	public Vehicle(String brand, String color) {
		this.brand = brand;
		this.color = color;
		this.speed = 0;
	}
	public String getBrand() {
		return brand;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public double getSpeed() {
		return speed;
	}
	public void setSpeed(double speed) {
		this.speed = speed;
	}
	public void run() {
		System.out.println("汽车奔跑,速度"+speed);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值