java程序设计基础_陈国君版第五版_第八章例题

Java编程基础:陈国君版第五版第八章实例解析
本篇博客详细探讨了《java程序设计基础》陈国君版第五版第八章的例题,涵盖了Java类(class)的使用、字符串操作以及编程设计实践。

java程序设计基础_陈国君版第五版_第八章例题

class Person{
	private String name;
	private int age;
	public Person(){
		System.out.println("调用了个人类的构造方法Person()");
	}
	public void setNameAge(String name , int age)
	{
		this.name = name ;
		this.age = age ;
	}
	public void show()
	{
		System.out.println("姓名:"+name+"\t年龄:"+age);
	}
	
}
class Student extends Person{
	private String department;
	public Student()
	{
		System.out.println("调用了学生类的构造方法Student()");
	}
	public void setDepartment(String dep)
	{
		department =dep;
		System.out.println("我是"+department+"的学生");
	}
}
public class Main8_1 {
	public static void main(String[] args)
	{
		Student stu =new Student();
		stu.setNameAge("张三",21);
		stu.show();
		stu.setDepartment("计算机系");
		
	}
}

class Person2{
	private String name;
	private int age;
	public Person2(){
		System.out.println("调用了个人类的无参构造方法Person()");
	}
	public Person2(String name,int age){
		System.out.println("调用了个人类的有参构造方法");
		this.name =name;
		this.age = age;
	}
	public void show()
	{
		System.out.println("姓名:"+name+"\t年龄:"+age);
	}
}
class Student2 extends Person2{
	private String department;
	public Student2()
	{
		System.out.println("调用了学生类的无参构造方法Student()");
	}
	public Student2(String name , int age ,String dep)
	{
		super(name,age);
		department = dep;
		System.out.println("我是"+department + "的学生");
		System.out.println("调用了学生类的有参构造方法Student(String name,int age ,String dep)");
	}
	
}
public class Main8_2 {
	public static void main(String[] args)
	{
		Student2 stu =new Student2();
		Student2 stu2 =new Student2("李四",23,"信息系");
		stu.show();
		stu2.show();
	}
}

class Person3
{
	protected String name ;
	protected int age ;
	public Person3()
	{
		
	}
	public Person3(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
	protected void show()
	{
		System.out.println("姓名:"+name+"\t年龄:"+age);
	}
}
class Student3 extends Person3{
	private String department;
	int age = 20;
	public Student3 (String xm,String dep)
	{
		name = xm ;
		department = dep;
		super.age = 25;
		System.out.println("子类Student中的成员变量 age ="+age);
		super.show();
		System.out.println("系别:"+department);
	}
}
public class Main8_3 {
	public static void main(String[] args){
		Student3 stu = new Student3("李四","信息系");
		
	}
}

class Person4
{
	protected String name ;
	protected int age ;
	public Person4(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
	protected void show()
	{
		System.out.println("姓名:"+name+"\t年龄:"+age);
	}
}
class Student4 extends Person4{
	private String department;
	public Student4(String name , int age ,String dep)
	{
		super(name,age);
		department = dep;
	}
	protected void show()
	{
		System.out.println("系别:"+department);
	}
}
public class Main8_4 {
	public static void main(String[] args)
	{
		Student4 stu = new Student4("老王",24,"电子");
		stu.show();
	}
}

class Person5
{
	protected String name ;
	protected int age ;
	public Person5(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
	protected void show()
	{
		System.out.println("姓名:"+name+"\t年龄:"+age);
	}
}
class Student5 extends Person5{
	private String department;
	public Student5(String name , int age ,String dep)
	{
		super(name,age);
		department = dep;
	}
	protected void show()
	{
		System.out.println("系别:"+department);
	}
	public void subShow()
	{
		System.out.println("我在子类中");
	}
}
public class Main8_5 {
	public	static void main(String[] args)
	{
		Person5 per = new Student5("王涛",24,"电子");
		per.show();
		//per.subshow();
	}
}

class AAA
{
	static final double PI = 3.14;
	public final void show()
	{
		System.out.println("pi = "+PI);
	}
}
class BBB extends AAA
{
	private int num = 100;
	/*public void show()
	{
		System.out.println("num = "+ num);
	}*/
}
public class Main8_6 {
	public static void main(String[] args){
		BBB ex = new BBB();
		ex.show();
	}
}

class A
{
	int a = 1;
}
public class Main8_7 {
	public static void main(String[] args){
		A obj1 = new A();
		A obj2 = new A();
		String s1,s2,s3 = "abc",s4 = "abc";
		s1 = new String("abc");
		s2 = new String("abc");
		System.out.println("s1.eauqls(s2)是"+(s1.equals(s2)));
		System.out.println("s1 == s3是"+(s1 == s3));
		System.out.println("s1.equals(s3)是"+(s1.equals(s3)));
		System.out.println("s3 == s4是"+(s3 == s4));
		System.out.println("s2.equals(s3)是"+(s2.equals(s3)));
		System.out.println("s1==s2是"+(s1 == s2));
		System.out.println("obj1 == obj2是"+(obj1 == obj2));
		System.out.println("obj1.equals(obj2)是"+(obj1.equals(obj2)));
		obj1 = obj2;
		System.out.println("obj1 = obj2后obj1 == obj2是"+(obj1==obj2));
		System.out.println("obj1 = obj2 后  obj1.equals(obj2) 是"+(obj1.equals(obj2)));
	}
}

class Person1
{
	protected String name;
	public Person1(String xm)
	{
		name = xm;
	}
}
public class Main8_8 {
	public static void main(String [] args)
	{
		Person1 per = new Person1("张三");
		Class obj = per.getClass();
		System.out.println("对象per 所属的类为:"+obj);
		System.out.println("对象per是否是接口:"+obj.isInterface());
	}
}

public class Main8_9
{
	static int count = 0;
	protected String name;
	protected int age;
	public Main8_9(String n1,int a1)
	{
		name = n1;
		age = a1;
		this.count++;
	}
	public String toString() 
	{
		return this.name+" , "+this.age;
	}
	public void display()
	{
		System.out.print("本类名 = "+this.getClass().getName()+";");
		System.out.println("父类名 = "+this.getClass().getSuperclass().getName());
		System.out.print("Main8_9.count = "+this.count+" ");
		System.out.print("Student9.count = "+Student9.count+" ");
		Object obj = this;
		if(obj instanceof Student9)
			System.out.println(obj.toString() + "是Student9类对象");
		else if(obj instanceof Main8_9)
			System.out.println(obj.toString() + "是Main8_9类对象");
	}
}
class Student9 extends Main8_9
{
	static int count = 0;
	protected  String dept;
	protected Student9(String n1,int a1,String d1)
	{
		super(n1,a1);
		dept = d1;
		this.count++;
	}
	public String toString()
	{
		return super.toString() +","+dept;
	}
	public void display()
	{
		super.display();
		System.out.print("super.count = "+super.count);
		System.out.println("\t;this.count="+this.count);
	}
	public static void main(String[] args){
		Main8_9 per = new Main8_9("王永涛",23);
		per.display();
		Student9 stu = new Student9("张小三",22,"计算机系");
		stu.display();
	}
}

abstract class Shape
{
	protected String name;
	public Shape(String xm)
	{
		name = xm;
		System.out.print("名称:"+name);
	}
	abstract public double getArea();
	abstract public double getLength();
}
class Circle extends Shape
{
	private final double PI = 3.14;
	private double radius;
	public Circle(String shapeName,double r)
	{
		super(shapeName);
		radius = r;
	}
	public double getArea()
	{
		return PI*radius*radius;
	}
	public double getLength()
	{
		return 2*PI*radius;
	}
}
class Rectangle extends Shape
{
	private double width;
	private double height;
	public Rectangle(String shapeName,double width,double height)
	{
		super(shapeName);
		this.width = width;
		this.height = height;
	}
	public double getArea()
	{
		return width * height;
	}
	public double getLength()
	{
		return 2*(width*height);
	}
}
public class Main8_10 {
	public static void main(String[] args){
		Shape rect = new Rectangle("长方形",6.5,10.3);
		System.out.print(";面积 = "+rect.getArea());
		System.out.println(";周长 ="+rect.getLength());
		Shape circle = new Circle("圆",10.2);
		System.out.println(";面积 = "+circle.getArea());
		System.out.println(";周长= "+circle.getLength());
	}

}

interface IShape
{
	final double PI = 3.14;
	abstract double getArea();
	abstract double getLength();
}
class Circle_11 implements IShape
{
	double radius;
	public Circle_11(double r)
	{
		radius = r;
	}
	public double getArea()
	{
		return PI*radius*radius;
	}
	public double getLength()
	{
		return 2*PI*radius;
	}
}
class Rectangle_11 implements IShape
{
	private double width;
	private double height;
	public Rectangle_11(double width,double height)
	{
		this.width = width;
		this.height = height;
	}
	public double getArea()
	{
		return width * height;
	}
	public double getLength()
	{
		return 2*(width + height);
	}
}
public class Main8_11 {
	public static void main(String[] args){
		IShape Circle_11 = new Circle_11(5.0);
		System.out.print("圆面积 ="+Circle_11.getArea());
		System.out.println(";周长 = "+Circle_11.getLength());
		Rectangle_11 rect = new Rectangle_11(6.5,10.8);
		System.out.print("矩形面积 ="+rect.getArea());
		System.out.println(";周长 = "+rect.getLength());
	}
}

interface Face1
{
	final double PI = 3.14;
	abstract double area();
}
interface Face2
{
	abstract void setColor(String c);
}
interface Face3 extends Face1,Face2
{
	abstract void volume();
}
public class Main8_12	implements Face3 {
	private double radius;
	private int height;
	protected String color ;
	public Main8_12(double r,int h)
	{
		radius = r;
		height = h;
	}
	public double area()
	{
		return PI*radius*radius;
	}
	public void setColor(String c)
	{
		color = c;
		System.out.println("颜色:"+color);
	}
	public void volume(){
		System.out.println("圆柱体体积 ="+area()*height);
	}
	public static void main(String[] args){
		Main8_12 volu = new Main8_12(3.0,2);
		volu.setColor("红色");
		volu.volume();
	}
}

interface Face1_13
{
	final double PI = 3.14;
	abstract double area();
}
interface Face2_13
{
	abstract void volume();
}
public class Main8_13 implements Face1_13,Face2_13{
	private double radius;
	private int height;
	public Main8_13(double r,int h)
	{
		radius = r;
		height = h;
	}
	public double area()
	{
		return PI*radius*radius;
	}
	public void volume()
	{
		System.out.println("圆柱体体积 ="+area()*height);
	}
	public static void main(String[] args){
		Main8_13 volu = new Main8_13(5.0,2);
		volu.volume();
	}
}

public class Main8_14 {
	private int age;
	public class Student
	{
		String name;
		public Student(String n,int a)
		{
			name = n;
			age = a;
		}
		public void output(){
			System.out.println("姓名:"+this.name+";年龄:"+age);
		}
	}
	public void output()
	{
		Student stu = new Student("刘洋",24);
		stu.output();
	}
	public static void main(String[] args){
		Main8_14 g = new Main8_14();
		g.output();
		
	}
}

public class Main8_15 {
	public static void main(String[] args){
		(
		new Inner()
		{
			void setName(String n)
			{
				name = n;
				System.out.println("姓名:"+name);
			}
		}
		).setName("张    华");	
	}
	static class Inner
	{
		String name;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值