51,410
社区成员
发帖
与我相关
我的任务
分享
class parent {
parent() {
j+=2;
}
public static int j = -1;
}
class child extends parent {
public int b;
child(int b) {
this.b=b;
}
}
public class Main {
public static void main(String[] args) throws Exception {
child b = new child(10);
System.out.println("parent:j="+b.j);//这里打印1是因为child(10)时,其父类parent()执行一次构造函数,静态变量j=j+2,j的值就成了1
System.out.println("child:b="+b.b);//这里是child(10)传进的10
child a = new child(10);
System.out.println("parent:j="+a.j);//这里打印的是3同上,其父类parent()执行一次构造函数,静态变量j=j+2,j的值就成了3
System.out.println("child:a="+a.b);
}
}
class child{
child(){
System.out.println("child:b=10");
}
}
class parent extends child{
parent(int j) {
System.out.println("parent:j="+j);
}
}
public class Main {
public static void main(String[] args) throws Exception {
new parent(1);
new parent(3);
}
}
public class Test091105_parent {
public Test091105_parent(){
}
public Test091105_parent(int i){
System.out.println("parent:i="+i);
}
public class Test091105_child extends Test091105_parent{
public Test091105_child(int i,char j,int k) {
super(i);
System.out.println("child:"+j+"="+k);
}
public Test091105_child(){
}
public class Test091105_main {
public static void main(String[] args) {
new Test091105_child(1,'b',10);
new Test091105_child(3,'a',10);
}
}
interface EqualDiagonal {
double getDiagonal();
}
class Rectangle implements EqualDiagonal {//矩形
double w;
double h;
public Rectangle() {
w = h = 0;
}
public void setW(double w) throws Exception {
if (w < 0) {
throw new Exception("");
}
this.w = w;
}
public void setH(double h) throws Exception {
if (h < 0) {
throw new Exception("");
}
this.h = h;
}
public double getDiagonal() {
return Math.sqrt(w * w + h * h);
}
}
class square extends Rectangle {
public boolean is_square;
square() {
}
public boolean isSquare() {
is_square = (w == h);
return is_square;
}
@Override
public double getDiagonal() {
if (isSquare()) {
return Math.sqrt(2 * w * w);
}
return 0;
}
}
public class NewMain {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
float w = 3, h = 4;
Rectangle r = null;
try {
r = new Rectangle();
r.setH(3);
r.setW(4);
} catch (Exception ex) {
}
System.out.println("" + r.getDiagonal());
square rr = new square();
try {
rr.setH(5);
rr.setW(5);
} catch (Exception ex) {
}
System.out.println("is square: "+rr.isSquare());
System.out.println("" + rr.getDiagonal());
}
}
class parent{
parent(){
}
parent(int b){
System.out.println("parent:b="+b);
}
}
class child extends parent{
child(int j,int b) {
super(b);
System.out.println("child:j="+j);
}
}
public class Main {
public static void main(String[] args) throws Exception {
new child(10,1);
new child(10,3);
}
}
class parent{
parent(){
}
parent(int b){
System.out.println("parent:b="+b);
}
}
class child extends parent{
child(int j,int b) {
super(b);
System.out.println("child:j="+j);
}
}
public class Main {
public static void main(String[] args) throws Exception {
new child(10,1);
new child(10,3);
}
}