JAVA读代码写程序

JAVA读代码写程序

package tttt;
class A{
	  int x=0,y=0;
	  A(){
	    System.out.println("now is in A's constructor");
	  }
	  A(int x,int y){
	    this.x = x;
	    this.y = y;
	  }
	  public void print(){
	    System.out.println("x="+x+";y="+y);
	  }
	}
	class B extends A{
	  B(){
	    System.out.println("now is in B's constructor");
	  }
	  B(int x,int y){
	  	super(x,y); 
	  }
	  public void print(){
	   super.print();
	   System.out.println("CCCCCCCCCCCCCCC"); 
	  }
	}
	public class Test1 {
	  public static void main(String[] args){
	    B  s = new B();
	    s.print();
	    B f = new B(10,20);
	    f.print();
	 }
	}

运行结果:
在这里插入图片描述

package tttt;
class C {
	private String name;
	public static String kind = "C";

	public C(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void think() {
		System.out.println(name + "think…");
	}

	public void like() {
		System.out.println("like");
	}
}
class D extends C {
	private String stuNo;
	public static String kind = "D";

	public D(String stuNo, String name) {
		super(name);
		this.stuNo = stuNo;
	}

	public void like() {
		System.out.println(stuNo + "号" + getName() + "like sport");
	}

	public void study() {
		System.out.println(stuNo + "号" + getName() + "study…");
	}
}

public class Test2 {
	public static void main(String[] args) {
		System.out.println("  ==== 1 =====");
		C c = new D("001", "Tom");
		System.out.println("该对象属于" + c.kind);
		c.like();
		c.think();
		System.out.println("\n  ==== 2 =====");
		D d = (D) c;
		System.out.println("该对象属于" + d.kind);
		d.like();
		d.think();
		d.study();
	}
}

运行结果
在这里插入图片描述

package tttt;
public class Test3 {
	public static void main(String[] args) {
		int iNum1 = 3;
		int iNum2 = 2;
		
		System.out.println(iNum1 | iNum2);
		System.out.println(++iNum1 < 5 || iNum2++ <3);
		System.out.println(iNum2);
}
}

运行结果:
在这里插入图片描述

package tttt;
public class Test4 {
	void test4(int i) {
		System.out.print("int");
	}

	void test4(String s) {
		System.out.print("String");
	}

	public static void main(String args[]) {
		Test4 crun = new Test4();
		char ch = 'h';
		int i = 12;
		crun.test4(ch);
		System.out.print(",");
		crun.test4(i);
	}
}

运行结果
在这里插入图片描述

package tttt;

public class Test5 {
	public static void main(String[] args) {
		Boy a = new Boy();
		System.out.println("Study");
	}
}
class Boy
{
	static
	{
		System.out.println("static Boy");
	}	
	public Boy()
	{
		System.out.println("Boy constructor");
	}
}

运行结果
在这里插入图片描述

本人公众号:

每天都会发一下些成程序员方面的知识,还有有用的工具在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java程序设计》课程的题库资料,由贺州学院整理,可供学生期末课程复习使用,也可以供相关任课教师出卷使用。 内容示例为: 9. 阅下列程序片段,出运行结果。 public class Test { public static void main(String[] args) { int percent = 10; tripleValue(percent); System.out.println(percent); } public static void tripleValue(int x) { x = 3 * x; } } 代码执行后输出的结果是______。 答案:10 [解析] static 关键字应用的场合有:①用来修饰类中定义的变量,这样的变量称为类变量或静态变量。②可以用来修饰类中定义的方法,这样的方法称为静态方法。③用来修饰初始化语句块,这样的语句块常称为静态初始化语句块。static 在这里表示这个方法为类方法,不属于任何对象实例,而是类所有,描述对象的共有动作,可以用类名直接调用。在调用了tripleValue函数之后,函数的值没有返回,所以percent的值还是10。 10. 阅下列程序片段,出运行结果。 class Shape { public Shape() { System.out.print("Shape"); } } class Circle extends Shape { public Circle() { System.out.print("Circle"); } } public class Test { public static void main(String[] args) { Shape d = new Circle(); } } 代码执行后输出的结果是______。 答案:ShapeCircle [解析] 继承是而向对象编程的一个主要优点之一,它对如何设计Java类有着直接的影响。继承有如下几点好处: ①它可以利用已有的类来创建自己的类,只需要指出自己的类和已有的其他类有什么不同即可,而且还可以动态访问其他有 关类中的信息。 ②通过继承,可以利用Java类库所提供的丰富而有用的类,这些类都已经被很好地实现。 ③当设计很大的程序时,继承可以使程序组织得层次清晰,有利于程序设计相减少错误的发生。该程序首先编了一个Shape的类,然后又编一个类Circle去继承Shape类。由于子类拥有父类所有的属性和方法,所以输出的是ShappeCircle。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值