public static void main(String[] args) {
/********** Begin **********/
//创建Dog对象
//设置Dog对象的属性
Dog wuhuarou = new Dog("五花肉","棕色","阿拉斯加");
//输出小狗的属性
System.out.println("名字:" + wuhuarou.getName() + ",毛色:" + wuhuarou.getColor() + ",品种:" + wuhuarou.getVariety() );
//调用方法
wuhuarou.eat();
System.out.println();
wuhuarou.run();
/********** End **********/
}
其他类
class Dog
//在这里定义Dog类
{
/********** Begin **********/
String name;
String color;
String variety;
public Dog(String name,String color,String variety)
{
this.name = name;
this.color = color;
this.variety = variety;
}
public Dog(){}
public void eat()
{
System.out.print("啃骨头");
}
public void run()
{
System.out.print("叼着骨头跑");
}
/********** End **********/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getVariety() {
return variety;
}
public void setVariety(String variety) {
this.variety = variety;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name = sc.next();
String sex = sc.next();
/********** Begin **********/
//分别使用两种构造器来创建Person对象
Person person = new Person();
Person person2 = new Person(name,sex);
/********** End **********/
}
其他
class Person
{
//创建Person对象,并创建两种构造方法
/********** Begin **********/
String name;
String sex;
public Person()
{
System.out.println("一个人被创建了");
}
public Person(String name, String sex) {
this.name = name;
this.sex = sex;
System.out.println("姓名:"+getName()+",性别:"+getSex()+",被创建了");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
/********** End **********/
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name = sc.next();
int age = sc.nextInt();
String sex = sc.next();
Person p = new Person(name,age,sex);
p.display();
}
class Person{
String name = "张三";
int age = 18;
String sex = "男";
/********** Begin **********/
public Person(String name,int age,String sex){
this(age);
this.name = name;
this.sex = sex;
}
public Person(int age){
this.age = age;
}
public void display(){
System.out.println("name:" +name);
System.out.println("age:" + age);
System.out.println("sex:" + sex);
}
/********** End **********/
}
注意这个题是分文件编写的
还有就是第一次可能出现问题(多半是网络问题),再试一次
/********** Begin **********/
//在这里添加包名 step4
package step4;
//创建类 添加属性和方法
public class WuMingFen {
String theMa ;
int quantity ;
boolean likeSoup ;
public WuMingFen() {
theMa = "酸辣";
quantity =2;
likeSoup =true;
}
public WuMingFen(String theMa,int quantity)
{
this.theMa = theMa;
this.quantity = quantity;
likeSoup = false;
}
public WuMingFen(String theMa, int quantity, boolean likeSoup) {
this.theMa = theMa;
this.quantity = quantity;
this.likeSoup = likeSoup;
}
public String getTheMa() {
return theMa;
}
public void setTheMa(String theMa) {
this.theMa = theMa;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public boolean isLikeSoup() {
return likeSoup;
}
public void setLikeSoup(boolean likeSoup) {
this.likeSoup = likeSoup;
}
public void check()
{
System.out.println("面码:" +getTheMa()+ ",粉的份量:"+getQuantity()+"两,是否带汤:"+likeSoup);
}
}
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String theMa = sc.next();
int quantity = sc.nextInt();
boolean likeSoup = sc.nextBoolean();
/********** Begin **********/
//使用三个参数的构造方法创建WuMingFen对象 取名 f1
WuMingFen f1 = new WuMingFen(theMa,quantity,likeSoup);
//使用两个参数的构造方法创建WuMingFen对象 取名 f2
WuMingFen f2 = new WuMingFen(theMa,quantity);
//使用无参构造方法创建WuMingFen对象 取名 f3
WuMingFen f3 = new WuMingFen();
//分别调用三个类的 check方法
f1.check();
f2.check();
f3.check();
/********** End **********/
}
}
public class Test {
/********** Begin **********/
static String name = "楚留香";
static
{
System.out.println("hello educoder");
}
public static void main(String[] args) {
System.out.println("我叫" + name);
study();
}
public static void study(){
System.out.println("我喜欢在educoder上学习java");
}
/********** End **********/
}