java实验报告Object and Inheritance
ReportClass:Q120101 Name:QianjunLiu No.:Q12010102CourseJavaProgrammingLanguageExperimentNameObject andInheritanceTypeVerify Demo Integrated DesignTeacher Weimin Wu Score1. Goal of this experimentI.Know how to use Object, inheritance in Java;ing Class, Object, Interface;III.U sing tools to construct and implement Java applications.2.ProceduresI. A program using Final, Abstract and Package.●Step 1: type the following code and try to get to know the usage ofFinal, Abstract and Package.●Step 2: Summary the function of this program and learn therelationship between class and object.●Step 3:Please tell the function of each line.II.A program using Interface.●Step 1: type the following code and try to get to know the usage ofInterface.●Step 2: Summary the function of this program and learn how touse Interface.●Step 3: Please tell the function of each line.III. A program using Data and TimeStep 1: type the following code and try to understand Data and Time in Java.●Step 2: Modify the source code to make it output different timeformat. Such as:Tue Sep 08 9:08:04 CST 2009 //toString2009-9-8 9:08:04 //toLocalString8 Sep 2009 01:08:04:GMT //toGMTString●Step 3: Think about the time gap between two Dates or two DataTimes. How to calculate. And trying to modify the source code to make it reality( extends or Interface can be used).A program using inheritance.●Step 1: Analyze the following code and give the runningresult:IV.Make a program which can represent student. This class have the following property: studentID, classID, name, sex and age.3.Recording of original data or result(1)a programme of circleSource code:class CCircle{double pi;double radius;double getRadius(){return radius;}void setCircle(double r, double p){pi=p;radius=r;}}public class Ex2_1{public static void main(String args[]) {CCircle cir1=new CCircle();cir1.setCircle(2.0,3.1416);System.out.println("radius="+cir1.getRadius());}}(2)A program using InterfaceSource code:interface Sittable{void sit();}interface Lie{void sleep();}interface HealthCare{void massage();}class Chair implements Sittable{public void sit(){System.out.println("It is sittable!");};}class Sofa extends Chair implements Lie,HealthCare{ public void sleep(){System.out.println("It is sleep!");};public void massage(){System.out.println("give a message!");};public static void main(String[] args) {Sofa cottonSofa=new Sofa();System.out.println(" Sofa is ok!");}}(3)A program using Data and TimeSource code:import java.util.Calendar;class Time {private Calendar t;private int y, m, d, hh, mm, ss;Time (){t=Calendar.getInstance();y=t.get(t.YEAR);m=t.get(t.MONTH)+1;d=t.get(t.DATE);hh=t.get(t.HOUR_OF_DAY);mm=t.get(t.MINUTE);ss=t.get(t.SECOND);}public String getDate() {return y+" 年"+m+"月"+d+"日";}public String getTime() {String s=hh+" 时"+mm+"分"+ss+"秒";return s;}public static void main(String args[]){Time t=new Time();System.out.println("当前日期:"+t.getDate());System.out.println("当前时间:"+t.getTime());}}(4)A program using inheritanceSource code:class SuperClass {int x;SuperClass() {x=3;System.out.println("in SuperClass : x=" +x);}void doSomething() {System.out.println("in SuperClass.doSomething()");}}class SubClass extends SuperClass {int x;SubClass() {super(); //调用父类的构造方法x=5; //super( ) 要放在方法中的第一句System.out.println("in SubClass :x="+x);}void doSomething( ) {super.doSomething( ); //调用父类的方法System.out.println("in SubClass.doSomething()");System.out.println("super.x="+super.x+" sub.x="+x);}}public class Inheritance {public static void main(String args[]) {SubClass subC=new SubClass();subC.doSomething();}}(5)Make a program which can represent student. This class have the following property: studentID, classID, name, sex and age.Source code:class Student{private long studentID;private int classID;private String name;private String sex;private int age;public Student(long studentID,int classID,String name,String sex,int age){this.studentID=studentID;this.classID=classID;=name;this.sex=sex;this.age=age;}public long getStudentID(){return studentID;}public int getClassID(){return classID;}public String getName(){return name;}public String getSex(){return sex;}public int getAge(){return age;}public String toString(){return "学号:"+getStudentID()+"\n班号:"+getClassID()+"\n姓名:"+getName()+"\n性别:"+getSex()+"\n年龄:"+getAge();}}public class StudentDemo{public static void main(String[] args){Student s1=newStudent(90221,2,"Tom","male",20);System.out.println(s1.toString());}}4.Result and analysis(1)a programme of circle(2)A program using Interface(3)A program using Data and Time(4)A program using inheritance(5)Make a program which can represent student. This class have the following property: studentID, classID, name, sex and age.Conclusion:Through this experiment, I come to understand class inheritance, three kinds of difference between the way of inheritance, and through the parameter list of the members of the derived class assignment method.The use of the virtual base class is to save a copy of base class data.What we should pay attention to is in the base class for other directly derived class .Signature:QianjunLiuDate:2014.10.26。
JAVA实验报告_Java类与对象
JAVA实验报告_Java类与对象贵州大学实验报告学院:计信学院专业:网络工程班级:101 姓名学号实验组实验时间05.11 指导教师肖源源成绩实验项目名称Java类与对象实验目的1)理解面向过程与面向对象区别2)理解面向对象的三大特征3)掌握类声明的方法4)掌握构造方法5)掌握对象的创建方法与创建机制6)掌握成员变量、类变量和局部变量区别7)掌握成员方法与类方法区别8)掌握函数的重载9)掌握4种访问修饰符的区别实验要求1.安装并设置Java SDK 软件包。
2.按要求编写程序完成实验指导所需实验要求。
3.调试并成功运行。
实验内容1、创建猴子类。
具体要求如下:1)编写一个类Monkey,用来表示猴子类;2)给Monkey类确定添加4个属性:3个实例成员变量(名字、体重、年龄)、1个静态成员变量(爱好:默认值为:吃香蕉);3)添加默认构造方法和有参构造方法(3个参数,分别给名字、体重和年龄赋初始值)4)编写展示猴子能够爬树(climbTree)的方法――提供两个参数传入:分别代码猴子的名字(name)和爬树的高度(height)(方法内只需打印猴子的”名字+会爬树,能爬+高度”)5)测试:在main方法中创建两个猴子对象monkey1(甲、50、3)和monkey2(乙、45、2),monkey1使用默认构造方法创建,monkey2使用有参构造方法创建。
分别打印出monkey1和monkey2的名字、体重、年龄和爱好。
并调用monkey1和monkey2的爬树方法。
2、创建矩形Rectangle类。
具体要求如下:1)创建一个Rectangle类;2)创建属性:两个double成员变量:width和height。
3)创建方法:计算矩形的周长getLength()和面积getArea()。
4)测试:创建Rectangle对象,并给两个成员变量赋值,调用周长和面积的方法,输出周长和面积。
实验数据实验总结整体实验基本达到要求,理解面向过程与面向对象区别,掌握类声明的方法,掌握对象的创建方法,掌握函数的重载等。
继承的应用实验报告
一、实验目的1. 理解继承的概念和作用。
2. 掌握Java中继承的实现方法。
3. 学习如何利用继承提高代码复用性。
4. 通过实验加深对继承的理解和应用。
二、实验环境1. 操作系统:Windows 102. 编译器:Eclipse IDE3. 编程语言:Java三、实验内容1. 创建一个基类:Animal,包含属性name和方法eat()。
2. 创建两个派生类:Dog和Cat,分别继承自Animal类,并添加各自特有的属性和方法。
3. 创建一个主类:TestInheritance,在主函数中创建Animal、Dog和Cat的对象,并调用它们的方法。
四、实验步骤1. 创建基类Animal:```javapublic class Animal {private String name;public Animal(String name) { = name;}public void eat() {System.out.println(name + " is eating."); }}```2. 创建派生类Dog:```javapublic class Dog extends Animal {private String breed;public Dog(String name, String breed) {super(name);this.breed = breed;}public void bark() {System.out.println(name + " is barking."); }}```3. 创建派生类Cat:```javapublic class Cat extends Animal {private String color;public Cat(String name, String color) {super(name);this.color = color;}public void meow() {System.out.println(name + " is meowing."); }}```4. 创建主类TestInheritance:```javapublic class TestInheritance {public static void main(String[] args) {Animal animal = new Animal("Animal");Dog dog = new Dog("Buddy", "Labrador");Cat cat = new Cat("Kitty", "Black");animal.eat();dog.eat();dog.bark();cat.eat();cat.meow();}}```五、实验结果与分析1. 运行主类TestInheritance,观察控制台输出:```Animal is eating.Buddy is eating.Buddy is barking.Kitty is eating.Kitty is meowing.```2. 分析结果:(1)Animal类作为基类,包含一个私有属性name和一个方法eat()。
java实验报告——对象和类
成绩:面向对象原理与Java实践课程实验报告实验1:对象和类姓名 ____班级 ____学号 ____实验地点 ____实验时间 ____指导教师 ____面向对象原理与Java实践课程实验报告实验1:对象和类一、实验目的:●掌握类的定义及应用方法●掌握对象的创建方法,了解实例变量、实例方法和静态字段的特性●体会Java在类和对象的设计中体现的面向对象的思想二、实验要求:●具备Java基础知识(教材第1、2章内容)●掌握Java中对象和类的基本概念和应用方法(教材第3章内容)●设计类时注意保证类的封装性●编写程序完成以下实验内容并上交实验报告(包括电子文档与纸质文档)三、实验内容:设计并实现一个“广石化院电信学院学生通信录”系统。
系统采用字符界面,在出现的主界面中按提示输入相应字符以选择“增加”、“删除”、“修改”、“查找”等功能进行操作(其中“查找”功能除能够查找某个学生的信息之外,还能够显示所有学生的信息)。
记录的存储可采用数组或链表的方式。
四、设计思路:为了实现该系统,首先要需要两个类;一个类为主界面服务,用于接收用户输入信息以及做出相关反应;一个类为学生信息服务,定义学生通讯录信息的各个字段,以及操作字段的各个方法,考虑到部分字段的属性问题,所以将操作方法都写在学生类中,不再另外放置在其他类中。
学生信息将会采用学生类的对象数组来储存。
五、程序源代码:下面是ZhuJieMian 主界面类package pkg_TXL;import java.util.Scanner;public class ZhuJieMian{/**** 该类为通讯录系统的主界面提供主要功能显示,以及负责主菜单与子菜单的正常跳转*/public static void main(String[] args){// TODO Auto-generated method stub//XueShengXinXi[] Stu=ChuShiHua(); //初始化系统数据XueShengXinXi[] Stu=new XueShengXinXi[100];Stu[0]=new XueShengXinXi("专业", "班级", "姓名", "性别", "电话", "家庭住址");Stu[1]=new XueShengXinXi("自动化", "自动化10-1", "张三", "男", "63256", "fnhjfdhsdbh");Stu[2]=new XueShengXinXi("自动化", "自动化10-2", "李四", "女", "653256", "nfhggdfddhsdbh");Stu[3]=new XueShengXinXi("电子", "电子10-1", "王五", "男", "6156", "fnhvjbh");Stu[4]=new XueShengXinXi("电子", "电子10-2", "小红", "女", "63956", "asdfnhjbh");Stu[5]=new XueShengXinXi("网络", "网络10-1", "小明", "男", "656", "nfgdgfnhjbh");Stu[6]=new XueShengXinXi("网络", "网络10-2", "小芳", "女", "62215", "nfgdmmmdfhbdsh");Stu[7]=new XueShengXinXi("计算机", "计算机10-1", "黄龙", "男", "65584", "bbbdddfnhjbh");Stu[8]=new XueShengXinXi("计算机", "计算机10-2", "陈玲", "女", "666114", "mjgfddddfnhjbh");Stu[9]=new XueShengXinXi("测控", "测控10-1", "赵小斌", "男", "644814", "fthnrehbkjhjbh");Stu[10]=new XueShengXinXi("测控", "测控10-2", "钱颖", "女", "63641", "dbfsvnnhjbh");Stu[11]=new XueShengXinXi("电信", "电信10-1", "孙武", "男", "6321514", "mmhmgvvdfnhjbh");Stu[12]=new XueShengXinXi("电信", "电信10-2", "李雨", "女", "6314", "43ghffvvdfnhjbh");for(int x=13;x<Stu.length;x++){Stu[x]=new XueShengXinXi();}System.out.println("欢迎使用广石化电信学院通讯录系统!");ZhuJieMian: for(int exit=0;exit==0;)//exit为退出主菜单的指示变量,0为不退出,非0为退出{System.out.println();System.out.println("************************************");System.out.println("****************主菜单****************");System.out.print(" 1。
西安邮电大学java类与对象实验报告
西安邮电⼤学java类与对象实验报告西安邮电⼤学(计算机学院)Java程序设计课内实验报告实验名称:类与对象专业名称:软件⼯程班级:学⽣姓名:学号(8位):指导教师:实验⽇期:2014年4⽉2⽇⼀. 实验⽬的及实验环境理解类与对象的概念,掌握Java 类的定义(域、⽅法)、创建对象和使⽤对象。
理解包的概念,会创建包,引⼊包。
掌握访问权限规则。
环境:eclipse⼆. 实验内容1. 设计Point 类⽤来定义平⾯上的⼀个点,⽤构造⽅法传递坐标位置。
默认构造⽅法创建坐标原点,带参数构造⽅法根据指定坐标创建⼀个点对象。
提供get、set ⽅法返回和设置坐标。
distance 返回两个点之间距离或当前点到指定坐标之间的距离。
同时设计应⽤类进⾏测试。
2. 设计⼀个三⾓形类,能判断给定三边是否构成三⾓形,能判断三⾓形的类型(普通、等腰、等边、直⾓,⽤枚举类型(参见第六章))能计算周长与⾯积。
并在应⽤类中演⽰。
3.定义⼀个Line 类,该类包含两个Point 类型的实例变量,⽤以表⽰线段的两个端点。
提供以下⽅法:计算线段长度;判断线段是否⽔平、判断是否为垂直、计算线段斜率、计算线段中点、判断两条线段是否相等。
并在应⽤类中演⽰。
4.定义两个包p1、p2,三个类C1、C2、C3.其中C1、C2 位于p1 中,C3 位于p2 中。
在C1 中定义四个不同访问控制修饰类型的变量,在C2、C3 中进⾏访问测试。
并练习在JDK 命令⾏下⽣成包。
三.⽅案设计对于第⼀题,⽤了staticstatic double distance(Point p1,Point p2){return Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));}对于第⼆题,采⽤if –else,严谨的判断三⾓形的形状public void judge(){if(a==b || b==c || c==a){if(a==b && b==c)System.out.println("该三⾓形为等边三⾓形。
java 类与对象实验报告
java 类与对象实验报告Java 类与对象实验报告一、实验目的本次实验的目的是通过实际操作,加深对Java类与对象的理解与掌握。
通过编写程序,创建类和对象,实现类的各种属性和方法,并进行相应的测试和调试,从而熟悉类与对象的概念、创建与使用。
二、实验内容本次实验包括以下几个方面的内容:1. 创建一个名为"Student"的类,该类包含学生的姓名、年龄、性别等属性,以及相应的get和set方法。
2. 创建一个名为"Course"的类,该类包含课程的名称、学分、教师等属性,以及相应的get和set方法。
3. 创建一个名为"Teacher"的类,该类包含教师的姓名、年龄、性别等属性,以及相应的get和set方法。
4. 在"Student"类中添加一个名为"selectCourse"的方法,该方法可以选择课程,并将其添加到学生的课程列表中。
5. 在"Student"类中添加一个名为"dropCourse"的方法,该方法可以退选课程,并将其从学生的课程列表中移除。
6. 在"Course"类中添加一个名为"showInfo"的方法,该方法可以显示课程的详细信息。
7. 在"Teacher"类中添加一个名为"teachCourse"的方法,该方法可以教授课程,并显示教师的相关信息。
三、实验过程首先,我们需要创建一个名为"Student"的类。
在该类中,我们定义了学生的姓名、年龄、性别等属性,并为每个属性添加了相应的get和set方法。
接着,我们在该类中添加了一个名为"selectCourse"的方法,该方法可以选择课程,并将其添加到学生的课程列表中;同时,我们还添加了一个名为"dropCourse"的方法,该方法可以退选课程,并将其从学生的课程列表中移除。
java常用的英语单词
java常用的英语单词
以下是10 个Java 中常用的英语单词及其意思:
1. Object(对象):在Java 中,一切皆对象。
对象是类的实例,它包含数据和操作这些数据的方法。
2. Class(类):类是对象的模板,它定义了对象的属性和方法。
3. Interface(接口):接口是一种特殊的类,它只包含方法的声明,而没有方法的实现。
4. Package(包):包是一种组织类和接口的方式,它将相关的类和接口组织在一起,以便更好地管理和使用。
5. inheritance(继承):继承是指一个类可以从另一个类中继承属性和方法。
6. Polymorphism(多态性):多态性是指不同的对象可以对同一方法进行不同的实现。
7. Encapsulation(封装):封装是指将对象的属性和方法封装在一起,只对外公开必要的接口。
8. Abstraction(抽象):抽象是指从具体的事物中提取出共同的特征和行为,形成一个抽象的概念。
9. Thread(线程):线程是程序中的执行单元,它可以并发地执行任务。
10. Exception(异常):异常是程序执行过程中出现的错误情况,Java 提供了异常处理机制来处理这些错误。
java类与对象实验报告
java类与对象实验报告Java类与对象实验报告一、引言Java是一种面向对象的编程语言,类和对象是Java编程的基本概念。
在本次实验中,我们将学习和掌握Java类和对象的概念、定义和使用方法,并通过实际的编程实验来加深对这些概念的理解。
二、实验目的1. 理解Java类和对象的概念;2. 学会定义和使用Java类;3. 掌握创建和操作Java对象的方法;4. 熟悉Java类和对象的相关语法和规范。
三、实验过程1. 类的定义在Java中,类是对象的模板,用于描述对象的属性和行为。
我们首先需要定义一个类,以便创建对象。
类的定义包括类名、属性和方法。
属性即对象的特征,方法即对象的行为。
2. 对象的创建通过使用关键字"new",我们可以创建一个类的对象。
对象是类的实例化,每个对象都有自己的属性和方法。
3. 对象的属性和方法对象的属性和方法可以通过对象名加点操作符来访问。
属性可以是基本类型或其他类的对象,方法可以是对象的行为或功能。
4. 构造方法构造方法是一种特殊的方法,用于创建对象时进行初始化操作。
构造方法的名称必须与类名相同,没有返回类型,可以有参数。
5. 封装性封装性是面向对象编程的重要特性之一,它将数据和方法封装在类中,对外部隐藏内部实现细节。
通过使用访问修饰符(private, public, protected)来控制属性和方法的访问权限。
6. 继承继承是面向对象编程的另一个重要特性,它允许我们创建一个新类,并从现有类中继承属性和方法。
通过使用关键字"extends"来实现继承。
7. 多态性多态性是面向对象编程的核心概念之一,它允许我们使用一个父类类型的引用来引用子类的对象。
通过方法的重写和重载来实现多态性。
四、实验结果通过本次实验,我们成功定义了一个Java类,并创建了多个对象。
我们可以通过对象名来访问对象的属性和方法,并对其进行操作。
我们还学习了构造方法的使用,以及封装性、继承和多态性的相关概念。
java第5节:inheritance
方法继承、覆盖与重载方法
没有明显地在子类中定义,它们也自动地从父类中继 承过来了。 承过来了
方法覆盖
子类定义同名方法来覆盖父类的方法,是多态技术的 子类定义同名方法来覆盖 类的方法 是多态技术的
一个实现。当父类方法在子类中被覆盖时,通常是子 类版本调用父类版本 并做一些附加的工作 类版本调用父类版本,并做 些附加的工作。
方法重载(overloading)
可以用相同的方法名但不同的参数表来定义方
法(参数表中参数的数量、类型或次序有差异 数 中 数 数 次序有 ),这称为方法重载。 编译器通过将在不同方法头部中的参数类型和 在特定的方法调用中使用值的类型进行比较, 从而挑选出正确的方法。
关于方法覆盖
Run-time 类信息
课后:
联机查看Java常用类的继承关系,了解文档的
使用方法。
在Java中利用关键字super子类可以调用超 类构造函数,也可以访问超类成员,当然 子类 可以直接调用超类中的方法 子类也可以直接调用超类中的方法。
this和super是常用来指代父类对象与子类对象的 关键字.
final修饰符的作用
与方法重写和继承的强大功能与广泛用途一样,有时也需 要阻止它们。例如,可能有 个封装了对某些硬件设备控 要阻止它们。例如,可能有一个封装了对某些硬件设备控 制的类。而且,这个类可能提供给用户初始化设备、使用 私有信息的能力。这种情况下,您会不希望类的用户重写 初始化方法 无论出于何种原因 在Java 初始化方法。无论出于何种原因,在 J 中 如果要防止 中,如果要防止 方法重写或类的继承,只需使用关键字final即可。 final关键字有三个用途
inheritance
子类的定义格式
[修饰符] { 《注意》 没有extends,默认父类为Object 只能有一个父类,即单继承 子类继承父类的全部成员 类继承具有传递性 子类名 类体; extends 父类名 }
子类与父类的属性与方法
子类继承父类的所有内容,但父类中的private部 分不能直接访问 子类中新增加的属性和方法是对父类的扩展 子类中定义的与父类同名的属性是对父类属性的隐 藏,同名的方法是对父类方法的重载或覆盖
类的继承----代码重用之道
在 Java 中,类是一个层次结构,其中有一个被 称为Object的特殊超类,所有的类都直接或间接 地继承ng.Object类 子类继承父类的属性和方法,同时也可以修改父 类的属性或重载父类的方法,以及在父类的基础 上添加新的属性和方法。因此,父类和子类 之 间反映的是“一般与特殊”的关系.
二、子类的构造Biblioteka 数子类继承父类无参构造函数 若子类无构造函数,则它将继承父类的无参构造 函数作为自己的构造函数;若子类定义了构造函 数,则在创建新对象时,先执行父类的无参构造 函数,再执行自己的构造函数 对于父类中含参构造函数,子类可以在自己的构 造函数中使用super调用它。
java中面对对象的英语
java中面对对象的英语English Answer:Object-oriented programming (OOP) is a programming paradigm that revolves around the concept of "objects." An object is a data structure consisting of data fields and methods together with their interactions. This makes it easier to create complex programs that are easier to maintain and reuse. OOP is based on several concepts such as encapsulation, abstraction, inheritance, and polymorphism. It aims to imitate and simplify the real world by programming objects that contain both data and functions.Encapsulation refers to the bundling of data and methods that operate on that data within a single unit. This helps keep data safe and secure from external interference and ensures that the object's internal state is not exposed.Abstraction is the act of hiding the implementation details of an object from the user. It allows the user to interact with the object without having to know how it works internally. This simplifies the design and maintenance of complex systems.Inheritance is a mechanism that allows objects to inherit properties and behaviors from other objects. It helps in code reusability and reduces redundancy.Polymorphism is the ability of an object to take on multiple forms. It allows objects to respond to the same message in different ways depending on their type.OOP has several advantages over other programming paradigms. It promotes code reusability, making it easier to maintain and update programs. It also enhances security by protecting data from unauthorized access. OOP makes programs more flexible and extensible, enabling them to adapt to changing requirements.However, OOP also has some disadvantages. It can bemore complex to design and implement than other programming paradigms. It also requires more memory and processing power, making it less suitable for resource-constrained environments.Overall, OOP is a powerful programming paradigm that has revolutionized software development. It has enabled the creation of complex, maintainable, and reusable software systems. However, it is important to understand its advantages and disadvantages before choosing it for a specific project.中文回答:面向对象编程 (OOP) 是一种编程范例,围绕“对象”的概念展开。
