Java两个类代码怎么写 java两个构造方法

java写两个类(A,B)要求A类有两个构造方法,私有成员,方法。B类调用A类方法打印出A类的私有数据

/*java中,有两个类A和B,B类中包含有参数构造方法b1和无参数方法b2,

成都创新互联是专业的庐阳网站建设公司,庐阳接单;提供做网站、网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行庐阳网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

* 那在A类中new个B类对象并调用方法b2。

* 那么new的时候是不是要加参数?

* 调用方法b2的时候构造方法是不是也调用了?*/

class A{

public A(){

System.out.println("System.out.println--调用无参的B的构造方法");

B b=new B();//如果需要调用B的无参构造方法,则不加参数

System.out.println("System.out.println--调用有参的B的构造方法");

B b2=new B(7);//如果需要调用有参的构造方法,则new的时候则添加参数,调用无参构造方法。

}

}

class B{

public B(){

System.out.println("调用无参构造方法");

}

public B(int b2){

System.out.println("调用有参构造方法");

}

}

public class TestABMain {

public static void main(String[] args) {

// TODO Auto-generated method stub

A a=new A();

}

}

代码如图:

运行结果如图:

(编写代码)Java创建两个类,一个植物类一个人类,静态属性和动态属性都自己定,然后测试属性自己定

public class C

{

public static void main(String[] args) throws Exception

{

Person p=new Manito("宙斯");

p.eat(new Watermelon());

System.out.println("可见大神也是人!");

}

}

abstract class Person

{

Person(String name)

{

this.name=name;

}

protected String getName()

{

return this.name;

}

public abstract void eat(Plant p);

private String name;

}

class Manito extends Person

{

Manito(String name)

{

super(name);

}

public void eat(Plant p)

{

System.out.println("大神"+this.getName()+"刚刚吃了:"+p.getType()+"。");

}

}

class Plant

{

Plant(String type)

{

this.type=type;

}

String getType()

{

return this.type;

}

private String type;

}

class Watermelon extends Plant

{

Watermelon()

{

super("西瓜");

}

}

java的代码中如果出现两个类,需要怎么做

在不同的package下是允许出现两个名字相同的类的,只要你在引入的时候注意就行了。但是尽量不要这么做,毕竟有风险

我刚学习java 如何在一个包中创建两个类?

你说的是java内部类的概念。好好去百度一下什么叫java内部类。内部类一般来说主要的作用是在类的内部创建一个嵌套类,并且该类一般是私有的。这样只有自己本身可以调用这个嵌套类里的方法。

你的代码如果要写的话如下:

public class circle

{

int x=7,y=8;

public void output()

{

System.out.println(x+" "+y);

cr.output1();

}

cross cr = new cross();

public static void main(String[] args) {

circle circle = new circle();

circle.output();

}

public class cross{

int a=4,b=6;

public void output1()

{

System.out.println("a="+a+" "+"b="+b);

}

}

}

编写一个Java程序,定义两个类!!!!!

"定义一个成员方法将a*b*c的结果输出带有主函数的类"

看不明白,这句话通么??

主函数到底是在一个新类还是在子类中?

把舌头捋直了说话

Java写一个接口和两个类,要求如下

//接口 IStudent.javapublic interface IStudent {

public double avgScore();

public void outputInfo();

} //类Studentpublic class Student implements IStudent {

private String sno;

private String name;

private int age;

public double[] scores;

public Student(String sno,String name,int age,double []score){

this.name = name;

this.scores= score;

this.age = age;

this.sno = sno;

}

public double avgScore() {

double total= 0;

for(int i=0,len=scores.length;ilen;i++){

total += scores[i];

}

return total;

} public void outputInfo() {

System.out.println("学号 "+sno+" 姓名 "+name +"年龄 "+age);

}}

//类 CollegeStudent大学生public class CollegeStudent extends Student {

private double dy;

private double ty;

private double my;

public CollegeStudent(String sno,String name,int age,double [] score,double dy,double ty,double my){

super(sno, name, age, score);

this.dy = dy;

this.ty = ty;

this.my = my;

}

public double calScore(){

return avgScore()+dy+ty+my;

}

public static void main(String []args){

double []score = {100,100,100,100,100};

CollegeStudent collegeStudent = new CollegeStudent("06411100","张三",20,score,100.0,100.0,100.0);

collegeStudent.outputInfo();

System.out.println("平均成绩 "+collegeStudent.avgScore());

System.out.println("综合成绩 "+collegeStudent.calScore());

}

}//


新闻标题:Java两个类代码怎么写 java两个构造方法
网站网址:http://myzitong.com/article/doohhhd.html