java,编写一个类,包含一定的方法,主函数测试类,实例化2个实例,求大神发个给我,谢谢谢谢
2个回答
展开全部
public class Demo {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void printInfo(){
System.out.println("输出:name="+this.name+",id="+this.id);
}
public static void main(String[] args) {
Demo demo1=new Demo();
Demo demo2=new Demo();
}
}
刚写的希望能帮到你!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/*
被实例化类A
*/
public class TestA {
private String name;
private int age;
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public void sayHi(){
System.out.println("大家好,我的名字叫'"+this.name+"',我今年"+this.age+"岁。");
}
}
/*
被实例化类B
*/
public class TestB{
private String name;
private int age;
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public void sayHi(){
System.out.println("大家好,我的名字叫'"+this.name+"',我今年"+this.age+"岁。");
}
}
/*
主函数
*/
public class Test{
public static void main(String[] args) {
TestA testa = new TestA();
testa.setName("张三");
testa.setAge(18);
testa.sayHi();
TestB testb = new TestB();
testb.setName("李四");
testb.setAge(20);
testb.sayHi();
}
}
被实例化类A
*/
public class TestA {
private String name;
private int age;
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public void sayHi(){
System.out.println("大家好,我的名字叫'"+this.name+"',我今年"+this.age+"岁。");
}
}
/*
被实例化类B
*/
public class TestB{
private String name;
private int age;
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public void sayHi(){
System.out.println("大家好,我的名字叫'"+this.name+"',我今年"+this.age+"岁。");
}
}
/*
主函数
*/
public class Test{
public static void main(String[] args) {
TestA testa = new TestA();
testa.setName("张三");
testa.setAge(18);
testa.sayHi();
TestB testb = new TestB();
testb.setName("李四");
testb.setAge(20);
testb.sayHi();
}
}
追问
谢谢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询