第一题无参
package test; import java.util.Random; import java.util.Scanner; public class hhh { public static void main(String[] args) { // TODO Auto-generated method stub notebook r=new notebook(); r.colourname=‘红‘; r.cpumodel=2; r.colour(); r.cpu(); } }
package test; public class notebook { char colourname; int cpumodel; public void colour(){ System.out.println("颜色是"+colourname); } public void cpu(){ System.out.println("型号是"+cpumodel); } }
有参
package test; import java.util.Random; import java.util.Scanner; public class hhh { public static void main(String[] args) { // TODO Auto-generated method stub notebook r=new notebook(); r.colour(‘红‘); r.cpu(2); } }
package test; public class notebook { public void colour(char colourname){ System.out.println("颜色是"+colourname); } public void cpu(int cpumodel){ System.out.println("型号是"+cpumodel); } }
测试类
package test; import java.util.Random; import java.util.Scanner; public class hhh { public static void main(String[] args) { // TODO Auto-generated method stub notebook r=new notebook(); r.colour(‘红‘); r.cpu(2); } }
第二题
package test; public class person { String name; double height; double weight; public void sayhellow(){ System.out.println("hellow,my name is"+name); } public void hhhhh(){ System.out.println("名字是"+name); System.out.println("升高是"+height); System.out.println("体重是"+weight); } }
测试类
package test; import java.util.Random; import java.util.Scanner; public class hhh { public static void main(String[] args) { // TODO Auto-generated method stub person r1=new person(); person r2=new person(); r1.height=1.73; r1.name="zhangsan"; r1.weight=56.3; r2.name="lishi"; r2.height=1.74; r2.weight=78.4; r1.sayhellow(); r1.hhhhh(); r2.sayhellow(); r2.hhhhh(); } }
原文:https://www.cnblogs.com/1353b/p/12759432.html