Enum public enum Week {Sunday, Moday, Tuesday, Wednesday, Thursday, Friday, Saturday} Week.Sunday.ordinal()  0 Week.Sunday  “Sunday”

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

Object Oriented Programming with Java
Introduction to Java Programming Lecture 10 Method Benefits, Declaring, and Calling Methods.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
More methods and classes, 4 of 4 Math 130 Introduction to Programming Lecture # 18 Monday, October 8, 2007.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Interfaces, Polymorphism II Lecture 21, Thu Mar
Introduction to Java Programming Lecture 13 Classes I OO Programming.
Introduction to Constructor, Overloading, and Accessibility CS340100, NTHU Yoshi.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Interfaces, Polymorphism Lecture 29, Fri Mar
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
Introduction to Java Programming Lecture 12 Method Benefits, Declaring, and Calling Methods.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
CS 241 – Computer Programming II Lab Kalpa Gunaratna –
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
Chapter 8: Inner Classes. You're an OO programmer, so you know that for reuse and flexibility/extensibility you need to keep your classes specialized.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Data Structures (1st Exam). 1.[5] Suppose there are only two constructors for a class, one that passes in a single integer parameter called amount, for.
Java Programming: Program Design Including Data Structures 1 Vectors The class Vector can be used to implement a list Unlike an array, the size of a Vector.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
C++ 程序语言设计 Chapter 9: Name Control. Outline  How to control storage and visibility by the static keyword  C++’s namespace feature  C++’s References.
Programming in Java CSCI-2220 Object Oriented Programming.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Java for C++ Programmers A Brief Tutorial. Overview Classes and Objects Simple Program Constructors Arrays Strings Inheritance and Interfaces Exceptions.
CONTENTS Wrapper Class Enumeration Garbage Collection Import static.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Interfaces and Inner Classes
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.
Introduction to C# By: Abir Ghattas Michel Barakat.
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
1 Enums (Chapter 4) To enumerate is: to name things one after another in a list Java has a type, called an enum, where a programmer specifies a finite.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance and Polymorphism
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
ENUMERATED TYPES Java Basics. Enumerated Type Topics Why use them? Defining an enumerated type Using an enumerated type Advanced features.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
OOP Tirgul 12.
Topic: Classes and Objects
Chapter 3: Using Methods, Classes, and Objects
CS 302 Week 11 Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
Chapter 9 Inheritance and Polymorphism
Classes & Objects: Examples
CS 302 Week 9 Jim Williams.
JAVA Constructors.
CIS 199 Final Review.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Corresponds with Chapter 5
Presentation transcript:

enum public enum Week {Sunday, Moday, Tuesday, Wednesday, Thursday, Friday, Saturday} Week.Sunday.ordinal()  0 Week.Sunday  “Sunday”

enum 不可繼承 1. public enum Suites exten Enum{ 2. } 可實作 interface 不能放在方法中 所有成員為 static final 可以用 == 或 equals 來比較 Compiler 會自動加入 private final 於列值中

Enum An enum specifies a list of constant values assigned to a type. An enum is NOT a String or an int. An enum constant's type is the enum type. An enum can be declared outside or inside a class, but NOT in a method. You can NEVER invoke an enum constructor directly.

The enum constructor is invoked automatically, with the arguments you define after the constant value. You can define more than one argument to the constructor, and you can overload the enum constructors An enum declared outside a class must NOT be marked static, final, abstract, protected, or private. Enum

enum constants can send arguments to the enum constructor, using the syntax BIG(8). Compiling an enum generates a.class file whose name is derived from the enum's name. The semicolon at the end of an enum declaration is optional. These are legal: enum Foo { ONE, TWO, THREE} enum Foo { ONE, TWO, THREE}; Foo.values() returns an array of MyEnum's values. Enum

enum Problem { BIG, HUGE, SMALL } // this cannot be private or protected class MyClass { Problem size; } public class MainClass { public static void main(String[] args) { MyClass drink = new MyClass(); drink.size = Problem.BIG; } } Declaring Enums

An example of declaring an enum inside a class class MyClass2 { enum Problem {BIG, HUGE, SMALL } Problem size; } public class MainClass { public static void main(String[] args) { MyClass2 drink = new MyClass2(); drink.size = MyClass2.Problem.BIG; // enclosing class name require d } }

You cannot declare enums in a method enum Problem {BIG, HUGE, SMALL } class MyClass { Problem size; } public class MainClass { public static void main(String[] args) { enum Problem { BIG, HUGE, SMALL } // WRONG! MyClass drink = new MyClass(); drink.size = Problem.BIG; } Exception in thread "main" java.lang.Error: Unresolved compilation problem: The member enum Problem cannot be local at MainClass.main(MainClass.java:9)

enum Problem { // 8, 10 & 16 are passed to the constructor BIG(8), HUGE(10), SMALL(16); Problem(int ounces) { // constructor this.ounces = ounces; } private int ounces; public int getOunces() { return ounces; } class MyClass { Problem size; public static void main(String[] args) { MyClass drink1 = new MyClass(); drink1.size = Problem.BIG; MyClass drink2 = new MyClass(); drink2.size = Problem.SMALL; System.out.println(drink1.size.getOunces()); for(Problem cs: Problem.values()) System.out.println(cs + " " + cs.getOunces()); } 8 BIG 8 HUGE 10 SMALL 16

Enums may have main() methods and can be invoked as applications. enum LightState { RED, YELLOW, GREEN; public static void main(String[] argv) { System.out.println(LightState.RED.toString()); } }

Enums have built-in name() and toString() methods, both of which return the name of the current instance. public class MainClass { public static void main(String[] argv) { System.out.println(LightState.RED.name()); } } enum LightState { RED, YELLOW, GREEN; }

You can add data, methods, and constructors to an enum. enum Suit { DIAMOND(true), HEART(true), CLUB(false), SPADE(false); private boolean red; Suit(boolean b) { red = b; } public boolean isRed() { return red; } public String toString() { String s = name(); s += red ? ":red" : ":black"; return s; } }