EducoderJava面向对象 – 类与对象

  • Post author:
  • Post category:java




EducoderJava面向对象 – 类与对象

1.什么是类,如何创建类

public class Test {
	public static void main(String[] args) {
		/********** Begin **********/
		//创建Dog对象
		//设置Dog对象的属性
		Dog wuhuarou = new Dog();
		wuhuarou.name = "五花肉";
        wuhuarou.color = "棕色";
        wuhuarou.variety = "阿拉斯加";
		//输出小狗的属性
		System.out.println("名字:" + wuhuarou.name + ",毛色:" + wuhuarou.color  + ",品种:" + wuhuarou.variety );
		
		//调用方法
        wuhuarou.eat();
        wuhuarou.run();	
	}
}
	class Dog{
    	String name;
    	String color;
    	String variety;
    	void eat(){System.out.println("啃骨头");}
    	void run(){System.out.println("叼着骨头跑");}
}

2.构造方法

import java.util.Scanner;

public class Test {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String name = sc.next();
		String sex = sc.next();
		/********** Begin **********/
		//分别使用两种构造器来创建Person对象  
		Person a = new Person();
        a.Person();
        a.Person(name,sex);
		/********** End **********/
		
	}
}

//创建Person对象,并创建两种构造方法
/********** Begin **********/
class Person{
    String name;
    String sex;
    public void  Person(){
        System.out.println("一个人被创建了");
    }
    public void  Person(String Name, String Sex){
        System.out.println("姓名:" + Name + ",性别:" + Sex + ",被创建了");
    }
}
/********** End **********/

3.选择题

C CD

4.This关键字

import java.util.Scanner;

public class Test {
	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.name = name;
		this.age = age;
		this.sex = sex;
	}
	
	public Person(int age){
		age = age;
	}
	
	public void display(){
		String name = "李四";
		int age = 11;
		String sex = "男";
		System.out.println("name:" + this.name);
		System.out.println("age:" + this.age);
		System.out.println("sex:" + this.sex);
	}
	/********** End **********/
}

5.类与对象练习

Test.java:

import java.util.Scanner;

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对象  取名 f2

		//使用无参构造方法创建WuMingFen对象  取名 f3

		//分别调用三个类的 check方法
        WuMingFen f1 = new WuMingFen(theMa,quantity,likeSoup);
		WuMingFen f2 = new WuMingFen(theMa,quantity);
		WuMingFen f3 = new WuMingFen();
		f1.cheak();
		f2.cheak();
		f3.cheak();
		
		/********** End **********/	
	}
}

WuMingFen.java

/********** Begin **********/
//在这里添加包名  step4
package step4;
//创建类 添加属性和方法
class WuMingFen{
    String theMa;
    int quantity;
    boolean likeSoup;
    public WuMingFen(String theMa, int quantity)
    {
    	this.theMa = theMa;
    	this.quantity = quantity;
    }
    public WuMingFen(String theMa, int quantity, boolean likeSoup)
    {
    	this.theMa = theMa;
    	this.quantity = quantity;
    	this.likeSoup = likeSoup;
    }
    public WuMingFen()
    {
        theMa = "酸辣";
        quantity = 2;
        likeSoup = true;
    }
    public void cheak(){
    	System.out.println("面码:" + this.theMa + ",粉的份量:" + this.quantity + "两,是否带汤:" + this.likeSoup);
    }
}
/********** End **********/	

6.static关键字

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 **********/
}

7.选择题

D EG B


仅供参考



版权声明:本文为qq_44921671原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。