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");
}
}
运行结果
本人公众号:
每天都会发一下些成程序员方面的知识,还有有用的工具