A library with Servo.print

Hi
I have made a simple circuit: a button connected with the pins 5V, 9 and GND. And I have created a library to program the Arduino UNO on which every time I push the button the Serial Monitor should write "on".
the file.h is

#ifndef LeggoM_h
#define LeggoM_h
#include "Arduino.h"
#include <SoftwareSerial.h>
class LeggoM
{
  public:
  LeggoM (int pin);
  void leggo();
  int val;
  private:
  int _pin, _val;
};
#endif

and the file.cpp is

#include "Arduino.h"
#include <SoftwareSerial.h>
#include "LeggoM.h"
LeggoM::LeggoM(int pin)
{
  _pin=pin;
}
void LeggoM::leggo()
{
    Serial.begin(9600);
    val=0;
    val=digitalRead(_pin);
    if (val==1)
    {
        Serial.print ("on\n");
     }
}

the file.ino is

#include <SoftwareSerial.h>
#include <LeggoM.h>
LeggoM Morse(9);
int mal;
void setup() 
{  

}

void loop() 
{  
  Morse.leggo();
}

But when I push the button the Serial Monitor writes casual numbers and letters. Why doesn't Serial.print work in the library ?

Repeatedly calling Serial.begin. Call it once in setup.

I find this thread's title confusing.

I have tried calling the Serial.begin in setup but it doesn't work.

ledservo:
I have tried calling the Serial.begin in setup but it doesn't work.

If you wrote the code with as little care as you wrote the title it is not suprising it does not work.

Modify your Original Post and fix the title.

...R

Why does your class include SoftwareSerial.h, when it never uses it?

But when I push the button the Serial Monitor writes casual numbers and letters.

WTF does this mean? Show the output that you get.

How is the switch wired?

What baud rate is the Serial Monitor set to?

Why doesn't Serial.print work in the library ?

Given that you don't print anything anywhere else, and that you get something in the Serial Monitor, I would think it obvious that this assumption is nonsense.