package balik; public class TopLevel1 {... }

Similar documents
Metody. public final class Ucet {... } public final void print() {... } tato metoda nemůže být překryta (overloaded) v

Java Lectures. Enhancements in Java 1.5

Avoiding magic numbers

Taming the Tiger. Joshua Bloch, Distinguished Engineer Neal Gafter, Senior Staff Engineer Sun Microsystems. java.sun.

CS 251 Intermediate Programming More on classes

COMP6700/2140 Enums. Alexei B Khorev and Josh Milthorpe. Research School of Computer Science, ANU. March 2017

Java Enum Java, winter semester ,2018 1

Taming the Tiger Joshua Bloch Neal Gafter

Topic 5: Enumerated Types and Switch Statements

JAVA V Assertions Java, winter semester

Sat 1/6/2018. MULTITHREAD... 5 THREADS What do you mean by Multithreaded program?... 5 LIFE CYCLE OF A THREAD:... 5

Chapter 1b Classes and Objects

Lab Solutions: Experimenting with Enumerated Types

CSC 207H Fall L Java Quiz Duration 25 minutes Aids allowed: none

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Everything You Ever Wanted To Know About Java O-O. 7-Dec-15

University of Palestine. Mid Exam Total Grade: 100

CS1004: Intro to CS in Java, Spring 2005

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

CompSci 125 Lecture 11

SUN Sun Certified Associate for the Java Platform.

Java Application Development

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Following is the general form of a typical decision making structure found in most of the programming languages:

OLLSCOIL NA heireann THE NATIONAL UNIVERSITY OF IRELAND, CORK. COLAISTE NA hollscoile, CORCAIGH UNIVERSITY COLLEGE, CORK

Data Reference Searcher. Documentation

Encapsulation in Java

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

COMP-202A, Fall 2007, All Sections Assignment 1

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

*Java has included a feature that simplifies the creation of

Some Sample AP Computer Science A Questions - Solutions

Introduction to Programming (Java) 4/12

CS180. Exam 1 Review

LECTURE 2 (Gaya College of Engineering)

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Object-Oriented Concepts

Functional Java Lambda Expressions

1.00/1.001 Introduction to Computers and Engineering Problem Solving Spring Quiz 1

COMP15111: Introduction to Architecture

Binghamton University. CS-140 Fall Functional Java

Common Mistakes with Functions. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Forthcoming Java Programming Language Features. Joshua Bloch and Neal Gafter Senior Staff Engineers Sun Microsystems, Inc.

QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Object Oriented Programming

CSE 142 Wi04 Midterm 2 Sample Solution Page 1 of 8

Binghamton University. CS-140 Fall Functional Java

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

University of Tokyo Java Class September 22-26, 2003 Forthcoming Java Language Features

ECE 122. Engineering Problem Solving with Java

Notes on Chapter Three

Nested Loops. A loop can be nested inside another loop.

A Monitor package for Java

CS 61B Discussion 5: Inheritance II Fall 2014

CS2 Assignment A1S The Simple Shapes Package

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Listen EECS /40

Class. Chapter 6: Data Abstraction. Example. Class

Computer Programming, I. Laboratory Manual. Final Exam Solution

cannot be used directly by the class However, a static method can create (or be given) objects, and can send messages to them

What is a class? Object Oriented Programming. What is a class? Classes. A class is primarily a description of objects, or instances, of that class

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

Space Exploration EECS /25

Introduction to the Java T M Language

Midterm Examination (MTA)

You have seen abstractions in many places, lets consider them from the ground up.

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

The data in the table are arranged into 12 rows and 12 columns. The process of printing them out can be expressed in a pseudocode algorithm as

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

Module Contact: Dr Anthony J. Bagnall, CMP Copyright of the University of East Anglia Version 2

Course information. Petr Hnětynka 2/2 Zk/Z

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

CP122 CS I. Iteration

Java Memory Management

CS 302 Week 9. Jim Williams

AP CS Unit 6: Inheritance Notes

CS 101 Spring 2007 Midterm 2 Name: ID:

CS211 Spring 2005 Prelim 1 March 10, Solutions. Instructions

Handout 4 Conditionals. Boolean Expressions.

Array. Prepared By - Rifat Shahriyar

CMP 326 Midterm Fall 2015

PROGRAMMING FUNDAMENTALS

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Options for User Input

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013

Course information. Petr Hnětynka 2/2 Zk/Z

Classes and Methods: Classes

JAVA OPERATORS GENERAL

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

CSC 240 Computer Science III Spring 2018 Midterm Exam. Name

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010


Exercise Session Week 7

Transcription:

Seminář Java Speciální třídy, výčtový typ Radek Kočí Fakulta informačních technologií VUT Březen 2010 Radek Kočí Seminář Java Speciální třídy, výčtový typ 1/ 20

Téma přednášky Vnořené třídy Anonymní třídy Výčtový typ (enums) Proměnný počet parametrů (varargs) Radek Kočí Seminář Java Speciální třídy, výčtový typ 2/ 20

Top-level classes Třídy nejvyšší úrovně (top-level classes) normální třídy jsou přímými členy nějakého balíku package balik; public class TopLevel1 {... Radek Kočí Seminář Java Speciální třídy, výčtový typ 3/ 20

Vnořené třídy (inner classes) Lokální třídy vnořené v jiné třídě (na úrovni lokálních proměnných) uváděné uvnitř bloku (platné pouze v uvedeném bloku) nesmí být public, private a protected Radek Kočí Seminář Java Speciální třídy, výčtový typ 4/ 20

Vnořené třídy (inner classes) public class TopLevel1 { private String text = "interni promenna"; public void test() { { class A { public A() { System.out.println(text); A a = new A(); // tady uz neni trida A dostupna Radek Kočí Seminář Java Speciální třídy, výčtový typ 5/ 20

Vnořené třídy (inner classes) Členské třídy (member classes) vnořené v jiné třídě (na úrovni vlastností objektu) public class TopLevel1 { private String text = "interni promenna"; class Inner { public Inner() { System.out.println("Inner: " + text); public m() { Inner i = new Inner();... Radek Kočí Seminář Java Speciální třídy, výčtový typ 6/ 20

Vnořené třídy (inner classes) Vnořené top-level třídy členské třídy s modifikátorem static vnořená rozhraní mohou být public, private a protected Radek Kočí Seminář Java Speciální třídy, výčtový typ 7/ 20

Vnořené třídy (inner classes) public class TopLevel1 { private String text = "interni promenna"; static public class TopLevel2 { public TopLevel2() { TopLevel1 t = new TopLevel1(); System.out.println(t.text); interface Cool {... Radek Kočí Seminář Java Speciální třídy, výčtový typ 8/ 20

Vnořené třídy (inner classes) Vnořené top-level třídy používá se k seskupení souvisejících tříd bez nutnosti vytvářet nový balík přístup k vnořeným top-level třídám (rozhraním) new TopLevel1.TopLevel2(); Radek Kočí Seminář Java Speciální třídy, výčtový typ 9/ 20

Anonymní třídy Anonymní třídy (anonymous classes) zvláštní případ vnořené třídy new Typ ( parametry ) { tělo anonymní třídy Typ představuje jméno konstruktoru rodičovské(!) třídy, od které je anonymní třída odvozena (následují jeho parametry) jméno rozhraní anonymní třída jako jediná může přímo instanciovat rozhraní (zde se parametry neuvádějí) Radek Kočí Seminář Java Speciální třídy, výčtový typ 10/ 20

Anonymní třídy class NejakaTrida { Runnable r = new Runnable() { public void run() { //... Radek Kočí Seminář Java Speciální třídy, výčtový typ 11/ 20

Enums Reprezentace výčtového typu (Java < 5.0) int Enum pattern public static final int SEASON WINTER = 0; public static final int SEASON SPRING = 1; public static final int SEASON SUMMER = 2; public static final int SEASON FALL = 3; není typově bezpečný hodnoty nejsou informativní (vždy int; jiné názvy, stejná hodnota) Radek Kočí Seminář Java Speciální třídy, výčtový typ 12/ 20

Enums Výčtový typ (Java >= 5.0) typově bezpečný enum Season {WINTER, SPRING, SUMMER, FALL plnohodnotná třída! rozšiřuje třídu java.lang.enum Radek Kočí Seminář Java Speciální třídy, výčtový typ 13/ 20

Enums class EnumsDemo { public enum Season { WINTER, SPRING, SUMMER, FALL ; private Season season; public static void main(string[] args) { new EnumsDemo(); public EnumsDemo() { season = Season.WINTER; System.out.println(season); for (Season s : Season.values()) System.out.println("Value of Season: " + s); Radek Kočí Seminář Java Speciální třídy, výčtový typ 14/ 20

Enums public enum Planet { MERCURY (3.303e+23, 2.4397e6), VENUS (4.869e+24, 6.0518e6), EARTH (5.976e+24, 6.37814e6); private final double mass; // in kilograms private final double radius; // in meters Planet(double mass, double radius) { this.mass = mass; this.radius = radius; Radek Kočí Seminář Java Speciální třídy, výčtový typ 15/ 20

Enums public static final double G = 6.67300E-11; double surfacegravity() { return G * mass / (radius * radius); double surfaceweight(double othermass) { return othermass * surfacegravity(); Radek Kočí Seminář Java Speciální třídy, výčtový typ 16/ 20

Enums public static void main(string[] args) { double earthweight = Double.parseDouble(args[0]); double mass = earthweight/planet.earth.surfacegravity(); for (Planet p : Planet.values()) System.out.printf("Your weight on %s is %f%n", p, p.surfaceweight(mass)); http://java.sun.com/j2se/1.5.0/docs/guide/ language/enums.html Radek Kočí Seminář Java Speciální třídy, výčtový typ 17/ 20

Varargs Varargs metoda s proměnným počtem parametrů pole objektů třídy Object Object[] arguments = { new Integer(7), new Date(), "a disturbance in the Force" ; String result = MessageFormat.format( "At {1,time on {1,date, there was {2 " + "on planet {0,number,integer.", arguments); Radek Kočí Seminář Java Speciální třídy, výčtový typ 18/ 20

Varargs Varargs (Java >= 5.0) vlastnost skrývající nutnost obalení polem poslední argument: Object... name Radek Kočí Seminář Java Speciální třídy, výčtový typ 19/ 20

Varargs class Args { public static void main(string[] args) { Args a = new Args(); a.print("hi ", "hou ", "ha"); public void print(string s, Object... args) { String str = ""; System.out.println(args.length); for (Object o : args) str += (String) o; System.out.println(s + str); Radek Kočí Seminář Java Speciální třídy, výčtový typ 20/ 20